Wednesday 5 March 2014

Pre-Packaged Applications

blogger
 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.(See part 3 of this series for 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:9440012345"));
startActivity(callNumber);

This will call the number 9440012345. 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);

No comments: