Android : Menus Part 3: Alternative Menus
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 ofContent Providerswhich is in brief the ability of an application to expose its data (stored in a database or a file) by defining aMIME typeto it and through a contentURIto be accessed by any other application through this URI.
For example if an application nameEmployeeshas some data of employees stored within this application context, and another application wants to access this data; thenEmployeesapplication should declare a content provider to expose its data, with aMIME type: vnd.android.cursor.item/mina.android.Employeesso any other application can access the employees data by calling the Uricontent://employees/Allto access all employees or the Uricontent://employees/1to 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.
Now we want to add an alternative menu item in an activity inApplication Aso that when clicked passes aURIof employees and launches an activity inApplication B(that manipulates the employees data according to the URI received).
So to add the alternative menu item in the activity ofApplication A, we write the following inonCreateOptionsMenumethod:
public boolean onCreateOptionsMenu(Menu menu) { //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;}
Here’s what we did:
- Create an intent with the desired action (Intent.ACTION_VIEW) on the specified content provider Uri (content://employees/All).
- CalladdIntentOptionsmethod with the specified parameters.
The above code adds a menu item that launches all possible activities in all applications on the device that can deal with the actionIntent.ACTION_VIEWon the Uri content://employees/All.
Now inApplication B, if we want it to handle such an intent we have to do the following.
Define anactivityin the application and specify in theAndroidManifest.xmlfile that this activity can handle the requests of the employees content provider like this:
<activity android:name=".ContentProvidersDemo" android:label="@string/app_name"><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>
The aboveIntentFiltermeans that this activity will respond t0 any implicit intent from any application with the following parameters:
- Action: Intent.ACTION_VIEW.
- Category: android.intent.category.ALTERNATIVE.
- Data of MIME type:vnd.android.cursor.item/mina.android.Employees.
So inApplication Awhen you press on the menu button, you’ll see a menu like this:
When you press on theAccess Employeesmenu item, the activity inApplication Bwill be launched.
That was Android Alternative menus, stay tuned for another tutorial next week and post any questions you may have in the comments.
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer