Adding Menu Items Based on an Intent

Adding Menu Items Based on an Intent


Sometimes you'll want a menu item to launch an activity using an Intent(whether it's an activity in your application or another application). When you know the intent youwant to use and have a specific menu item that should initiate the intent, you can execute theintent with startActivity() during theappropriate on-item-selected callback method (such as the onOptionsItemSelected() callback).

However, if you are not certain that the user's devicecontains an application that handles the intent, then adding a menu item that invokes it can resultin a non-functioning menu item, because the intent might not resolve to anactivity. To solve this, Android lets you dynamically add menu items to your menuwhen Android finds activities on the device that handle your intent.

To add menu items based on available activities that accept an intent:

  1. Define anintent with the category CATEGORY_ALTERNATIVE and/orCATEGORY_SELECTED_ALTERNATIVE, plus any other requirements.
  2. Call Menu.addIntentOptions(). Android then searches for any applications that can perform the intentand adds them to your menu.

If there are no applications installedthat satisfy the intent, then no menu items are added.

Note:CATEGORY_SELECTED_ALTERNATIVE is used to handle the currentlyselected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu().

For example:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);

    // Create an Intent that describes the requirements to fulfill, to be included
    // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.               
// 如下:

// Allowing your activity to be added to other menus

// 注意:这里是用隐式intent 匹配的,如果使用显示intent 匹配,则无论 the offering app 是否包含  intent.category_alternative, 都会显示在menu中。   Intent intent = new Intent(null, dataUri);   intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // Search and populate the menu with acceptable offering applications. menu.addIntentOptions( R.id.intent_group, // Menu group to which new items will be added 0, // Unique item ID (none) 0, // Order for the items (none) this.getComponentName(), // The current activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null); // Array of MenuItems that correlate to specific items (none) return true; }

For each activity found that provides an intent filter matching the intent defined, a menuitem is added, using the value in the intent filter's android:label as themenu item title and the application icon as the menu item icon. TheaddIntentOptions() method returns the number of menu items added.

Note: When you call addIntentOptions(), it overrides any and all menu items by the menu group specified in the firstargument.

Allowing your activity to be added to other menus

You can also offer the services of your activity to other applications, so yourapplication can be included in the menu of others (reverse the roles described above).

To be included in other application menus, you need to define an intentfilter as usual, but be sure to include the CATEGORY_ALTERNATIVEand/or CATEGORY_SELECTED_ALTERNATIVE values for the intent filtercategory. For example:

<intent-filter label="@string/resize_image">
    ...
    <category android:name="android.intent.category.ALTERNATIVE" />
    <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
    ...
</intent-filter>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值