Android offers a third type of menus: Alternative menus which allow multiple applications to use each other. An application menu can contain menu items that point to other applications that deal with a certain data type that is passed from the application by an intent.
This functionality is related to the concept of Content Providers which is in brief the ability of an application to expose its data (stored in a database or a file) by defining a MIME type to it and through a content URI to be accessed by any other application through this URI.
For example if an application name Employees has some data of employees stored within this application context, and another application wants to access this data; then Employees application should declare a content provider to expose its data, with a MIME type:
vnd.android.cursor.item/mina.android.Employees so any other application can access the employees data by calling the Uri content://employees/All to access all employees or the Uri content://employees/1 to access a single employee instance (for example).
Back to our Alternative menus issue, suppose this Scenario: We have two applications:
So to add the alternative menu item in the activity of Application A, we write the following in onCreateOptionsMenu method:
Now in Application B, if we want it to handle such an intent we have to do the following.
Define an activity in the application and specify in the AndroidManifest.xml file that this activity can handle the requests of the employees content provider like this:
When you press on the Access Employees menu item, the activity in Application B will be launched.
This functionality is related to the concept of Content Providers which is in brief the ability of an application to expose its data (stored in a database or a file) by defining a MIME type to it and through a content URI to be accessed by any other application through this URI.
For example if an application name Employees has some data of employees stored within this application context, and another application wants to access this data; then Employees application should declare a content provider to expose its data, with a MIME type:
vnd.android.cursor.item/mina.android.Employees so any other application can access the employees data by calling the Uri content://employees/All to access all employees or the Uri content://employees/1 to access a single employee instance (for example).
Back to our Alternative menus issue, suppose this Scenario: We have two applications:
- Application A: deals with the employees data.
- Application B: receives the content Uri of the employees and do some calculations on it.
So to add the alternative menu item in the activity of Application A, we write the following in onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {Heres what we did:
//adds a regular menu item
menu.add("Regular item");
//create the intent with the Uri of the employees content provider
Intent targetIntent=
new Intent(Intent.ACTION_VIEW, Uri.parse("content://employees/All"));
targetIntent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, //item group
Menu.CATEGORY_ALTERNATIVE, //item id
Menu.CATEGORY_ALTERNATIVE, //item order
this.getComponentName(), //our activity class name
null, //no specific menu items required
targetIntent, // the intent to handle
0, //no flags
null); //Optional array in which to place the menu
//items that were generated for each of
//the specifics that were requested
return true;
}
- Create an intent with the desired action (Intent.ACTION_VIEW) on the specified content provider Uri (content://employees/All).
- Call addIntentOptions method with the specified parameters.
Now in Application B, if we want it to handle such an intent we have to do the following.
Define an activity in the application and specify in the AndroidManifest.xml file that this activity can handle the requests of the employees content provider like this:
<activity android_name=".ContentProvidersDemo" android_label="@string/app_name">The above IntentFilter means that this activity will respond t0 any implicit intent from any application with the following parameters:
<intent-filter android_label="Access Employees">
<action android_name="android.intent.action.VIEW" />
<category android_name="android.intent.category.DEFAULT" />
<category android_name="android.intent.category.ALTERNATIVE" />
<data android_mimeType="vnd.android.cursor.item/mina.android.Employees" />
</intent-filter>
</activity>
- Action: Intent.ACTION_VIEW.
- Category: android.intent.category.ALTERNATIVE.
- Data of MIME type:vnd.android.cursor.item/mina.android.Employees.
When you press on the Access Employees menu item, the activity in Application B will be launched.
0 comments:
Post a Comment