Auto-generate Flow Chart from Java/C++ Codes:

Raptor Flowchart Tutorial For Beginners

Monday, July 23, 2012

Android Intent (Implicit)



An implicit intent does not name a target component that should act upon the intent. I  also said that the android platform resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?

Basically, an Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:
  • Action 
  • Category 
  • Data 
So, the android platform compares these 3 (action, category and data) to something called "Intent Filters" that are declared by probable target components who are willing to accept Implicit Intent calls.
 i.e. Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.

So here are some important points to remember:
  1. Implicit Intents do not specify a target component 
  2. Components willing to receive implicit intents have to declare their ability to handle a specific intent by declaring intent filters 
  3. A component can declare any number of Intent Filters 
  4. There can be more than one component that declares the same Intent Filters and hence can respond to the same implicit intent. In that case the user is presented both the component options and he can choose which one he wants to continue with 
  5. You can set priorities for the intent filters to ensure the order of responses. 

There are 3 tests conducted in order to match an intent with intent filters:
  1. Action Test 
  2. Category Test 
  3. Data Test 
For more details about them, you may visit the Android developer documentation here.

Finally we shall look at declaring an implicit intent in one activity which will invoke one of the native activities of the platform by matching the intent filters declared by the same.

The complete code for a very simple implicit intent example that has been described in this article is available for download here.

The InvokeImplicitIntent Activity creates an implicit intent object "contacts". This intent object's component is not set. However, the action is set to "android.content.intent.ACTION_VIEW" and the data's URI is set to "People.CONTENT_URI". 

Such an intent matches with the intent filter declared by the view contacts native activity.
So, when you run this application, it displays the native UI for viewing the existing contacts on the phone!

Here is the relevant piece of code for the same:

01.Button viewContacts = (Button)findViewById(R.id.ViewContacts);
02. 
03.viewContacts.setOnClickListener(new OnClickListener() {
04. 
05.public void onClick(View v) {
06.Intent contacts = new Intent();
07.contacts.setAction(android.content.Intent.ACTION_VIEW);
08.contacts.setData(People.CONTENT_URI);
09.startActivity(contacts);
10.}
11.});

In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.

---------------------------------------------------------------------------------------------------------
Updated for Android SDK 1.6 and above:
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
      
1.contacts.setData(People.CONTENT_URI);

has to be replaced by 

1.contacts.setData(ContactsContract.Contacts.CONTENT_URI);


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




from:http://mobile.dzone.com/articles/implicit-intent-android

No comments: