博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android : Menus Part 3: Alternative Menus
阅读量:5796 次
发布时间:2019-06-18

本文共 3879 字,大约阅读时间需要 12 分钟。

hot3.png

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 typeto 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; thenEmployees 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:

  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 in Application A so that when clicked passes a URI of employees and launches an activity in Application B (that manipulates the employees data according to the URI received).

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) {    	//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).
  • Call addIntentOptions method 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 action Intent.ACTION_VIEW on the Uri content://employees/All.

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:

The above IntentFilter means 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 in Application A when you press on the menu button, you’ll see a menu like this:

When you press on the Access Employees menu item, the activity in Application B will be launched.

That was Android Alternative menus, stay tuned for another tutorial next week and post any questions you may have in the comments.

转载于:https://my.oschina.net/artshell/blog/390089

你可能感兴趣的文章
自动化部署之jenkins发布PHP项目
查看>>
C/C++编程可用的Linux自带工具
查看>>
Maven介绍与安装配置
查看>>
[Zabbix问题解决]Zabbix discoverer processes 75% busy
查看>>
.net 2.0 BackgroundWorker类详细用法
查看>>
Eclipse快捷键指南(转自蓝法典的专栏)
查看>>
ATI X300 显卡在 Edgy 上使用 aiglx 指南
查看>>
二十年后的回眸(9)——十年著书路
查看>>
《Python爬虫开发与项目实战》——2.2 HTTP标准
查看>>
docker 任意端口启动jenkins (CentOs7 Docker 17.03.1-ce)
查看>>
线程间共享数据无需竞争
查看>>
从航空母舰上起飞,是怎样一种体验----EDAS带你快速搞定分布式应用
查看>>
容器就像监狱,让我们来构造一个监狱吧!(含代码下载)
查看>>
三种数据分析法提升电商运营
查看>>
哪个线程执行 CompletableFuture’s tasks 和 callbacks?
查看>>
《数据科学与大数据分析——数据的发现 分析 可视化与表示》一2.10 练习
查看>>
Oracle ASM 翻译系列第六弹:高级知识 如何映射asmlib管理的盘到它对应的设备名...
查看>>
多线程之volatile关键字
查看>>
如何判断webview是不是滑到底部
查看>>
Raptor实践2——控制结构
查看>>