Pages

Monday, February 22, 2016

Android Intents Part3 Intent Filters

Android components like activities can also serve implicit intents. but to do so they have to filter all implicit intents in order to serve only the intents they desire to serve. this is done using intent filters.

Suppose you want to create an activity that acts as the default dialer activity. You must associate an intent filter with this activity in order to that this activity serve the dial intents only.

Lets demonstrate a simple example which is creating an application with one activity that we want to make it a dialer activity.

Create a new android project, create an activity and name it Dialer.

In the AndroidManifest.xml file of this application add the following to the Dialer activity:
<intent-filter android_priority="100" >
<action android_name="android.intent.action.DIAL" />
<category android_name="android.intent.category.DEFAULT" />
<data android_scheme="tel"/>
</intent-filter>
to become:
<activity android_name=".Dialer"
android_label="@string/app_name">
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android_priority="100" >
<action android_name="android.intent.action.DIAL" />
<category android_name="android.intent.category.DEFAULT" />
<data android_scheme="tel"/>
</intent-filter>
</activity>
Now what we have done is adding an intent filter to that activity. this intent filter has the following properties:
  1. Action: the type of implicit intents that this activity responds to. in our case it is the dial action. higher numbers represent higher priority.
  2. Priority: about the priority of that activity over other activities that respond to the same type of intents.
  3. Category: Implicit intents have built-in categories. in order that an implicit intent be captured by our activity, the implicit intent category must match our activity category.
  4. Data: adds data specification scheme to the intent filter.
So if any other application has the following module to launch the dialer:
Intent in=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000"));
startActivity(in);
The user will see the following dialog offering him/her the choice between the default dialer and our custom dialer.

Related Posts by Categories

0 comments:

Post a Comment