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:

  1. Application A: deals with the employees data.
  2. 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:

  1. Action: Intent.ACTION_VIEW.
  2. Category: android.intent.category.ALTERNATIVE.
  3. 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:

Android : Menus Part 3: Alternative Menus

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.


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android的事件处理机制之基于监听的事件
  2. [置顶] 整个网络可能最完善的 Android(安
  3. 监听Android设备网络变化 - 亲测在华为Em
  4. KCommon-使用Kotlin编写,基于MVP的极速开
  5. Android数据与界面绑定工具简述
  6. 学Android开发不可不知的Android应用程序
  7. Gradle Kotlin DSL , 你知道它吗?
  8. Android(安卓)Stuido部分快捷键失灵
  9. Android定制视图及手势检测的基本示例
  10. Android(安卓)MVC 架构详解