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.
Let's 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:
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.
Let's 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" >to become:
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel"/>
</intent-filter>
<activity android:name=".Dialer"Now what we have done is adding an intent filter to that activity. this intent filter has the following properties:
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>
- Action: the type of implicit intents that this activity responds to. in our case it is the dial action. higher numbers represent higher priority.
- Priority: about the priority of that activity over other activities that respond to the same type of intents.
- 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.
- Data: adds data specification scheme to the intent filter.
Intent in=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000"));The user will see the following dialog offering him/her the choice between the default dialer and our custom dialer.
startActivity(in);
No comments:
Post a Comment