Saturday, November 24, 2012

Calling system applications in Android

In my previous posts I have used reading contacts through API calls, however there is an easy method for doing standard tasks by calling system's pre installed apps. In this post I am going to cover the details of how we can call the system apps through intents.


We are going to create a spinner control for providing the user some options and hit a submit button.

Here's main.xml which defines the UI.

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
     >  
   <Spinner  
       android:id="@+id/spinner1"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:entries="@array/application_array"  
       android:prompt="@string/app_prompt" />  
   <Button  
       android:id="@+id/btnSubmit"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="Submit"  
       android:onClick="btnSubmitOnClick"/>  
 </LinearLayout>  


Spinner control requires a string array (application_array) for it's entries which is defined in strings.xml.

 <?xml version="1.0" encoding="utf-8"?>  
 <resources>  
   <string name="app_name">callingsystemapps</string>  
   <string name="app_prompt">Choose a application</string>  
   <string-array name="application_array">  
     <item>Browser</item>  
     <item>Dialler</item>  
     <item>Map</item>  
     <item>Contacts</item>  
   </string-array>  
 </resources>  


In main.xml we have also have defined onClick method btnSubmitOnClick which is defined in MyActivity.java

   public void btnSubmitOnClick (View v)  
   {  
     Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);  
     if (spinner1.getSelectedItem().toString().equals("Browser")){  
       Intent i = new  
           Intent(android.content.Intent.ACTION_VIEW,  
           Uri.parse("http://www.google.com"));  
       startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Dialler")){  
       Intent i = new  
           Intent(android.content.Intent.ACTION_DIAL);  
       startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Map")) {  
         Intent i = new  
             Intent(android.content.Intent.ACTION_VIEW,  
             Uri.parse("geo:37.827500,-122.481670"));  
         startActivity(i);  
     }  
     else if (spinner1.getSelectedItem().toString().equals("Contacts")) {  
       Intent i = new  
           Intent(android.content.Intent.ACTION_PICK);  
       i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);  
       startActivity(i);  
     }  


Only difference between the four is the passing intent which is as below:

ACTION_VIEW
ACTION_DIAL
ACTION_PICK


Along with that we are passing parameters depending upon intent type. After that it's just calling startActivity on the intent.

Try running the example and you shall get like this.




Thanks for reading, don't forget to check links page for resources.

10 comments:

  1. I am always used to read this kind of blog from I could learn many this and it extend me from basic to advance level.

    ReplyDelete
  2. Great post and nice application developed for the calling system that makes their task easier. Great job. Thanks for sharing.
    Android apps development

    ReplyDelete
  3. I got an after reading this. I am developing Way2sms login app, I will call Messaging system app from it

    ReplyDelete
  4. Thanks all for your nice comments. :)

    ReplyDelete
  5. Thanks for this great tutorial on calling system applications. Im creating a scheduler app

    ReplyDelete
  6. this tutorial was really helpful in creating my android application. you can find more information on

    http://way2smshub.com/way2sms-mobile-apps/

    ReplyDelete
  7. Congrats for making it possible for creating this blog. I may not always visit this blog to see more updates but i will consider every valuable information i read here. Please keep up the good work and good luck for the upcoming post.

    Way2sms login

    ReplyDelete
  8. Great post having information and script of Calling system applications in Android. great work you have done. I am also working for Mobile Apps Development and your post really helps me a lots. Keep sharing..!!!

    ReplyDelete
  9. I want to learn more about Calling system applications in Android. Any good resource?
    Mike
    Offshore app developer

    ReplyDelete