Tuesday, April 10, 2012

System applications in android

There are many applications that can pre-packaged into the android platform that can be reused by custom built applications because of the power of the android design that is based on implicit intents.


Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:

1. Call a number
      Intent callNumber = new Intent();
      callNumber.setAction(android.content.Intent.ACTION_CALL);
      callNumber.setData(Uri.parse("tel:          0000000000      "));
startActivity(callNumber);

This will call the number             0000000000      . For calling custom numbers, you could provide the user with a edit text field from which you can access the number and set it to the data above instead of a hard-coded number.

2. Browse the web for a given url:

      Intent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
      searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
      startActivity(searchGivenText);

This will invoke the Google search engine to search the string "Android Examples" and return the results to you. This too can be generalized to accept a string from the user and set to the intent before starting the activity.

3.  View google maps for a given location

      Intent searchAddress = new Intent(Intent.ACTION_VIEW, 
      Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);

This shows the location of Bangalore on Google Maps.

4. View Contacts on the phone

Intent contacts = new Intent();
      contacts.setAction(android.content.Intent.ACTION_VIEW);
      contacts.setData(People.CONTENT_URI);
      startActivity(contacts);


I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.
--------
Updated on March 31 2010:

The above example uses Android SDK 1.5 From SDK 1.6 and above, the Contact.People class has been deprecated and we need to use the ContactsContract class. So the line in code
      contacts.setData(People.CONTENT_URI);
has to be replaced by 
              contacts.setData(ContactsContract.Contacts.CONTENT_URI);

Here is the complete source code that has been tested with Android SDK 2.1

1 comment:

  1. This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
    healthcare, e commerce, programming, multi platform,inventory management, cloud-based solutions, it consulting, retail, manufacturing, CRM, technology means, digital supply chain management, Delivering high-quality service for your business applications, Solutions for all Industries,packaged applications,business applications, Web services,
    Business intelligence, Business Development, Software Development etc.


    Our address:
    2002 Timberloch Place, Suite 200
    The Woodlands, TX 77380
    281-364-1799

    prologic-corp

    ReplyDelete