示例 NotesList(由 google 提供,在此处查看:Note Pad)展示了如何使用意图过滤器。
在 NotesList.java 中,此活动创建一个选项菜单,该菜单根据接受如下意图的可用活动添加菜单项:
@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.
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;
}
这个内容的细节是这样的:
操作:null 类型:null 数据(uri):content://com.google.provider.NotePad/notes 类别:android.intent.category.ALTERNATIVE
我希望我的活动包含在菜单中,并且我的活动的意图过滤器是这样的:
<intent-filter android:label="hello filter">
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="content" android:host="com.google.provider.NotePad"
android:path="notes" />
</intent-filter>
它不起作用。而如果我将数据更改为:
< data android:mimeType="vnd.android.cursor.dir/vnd.google.note" / >
,它会起作用。
这让我感到困惑,因为根据intents-filters:
包含 URI 但没有数据类型(并且无法从 URI 推断出类型)的 Intent 对象只有在其 URI 与过滤器中的 URI 匹配并且过滤器同样未指定类型时才能通过测试。这只适用于不引用实际数据的 URI,例如 mailto: 和 tel:。
notesList 中的意图仅包含一个 URI(content://com.google.provider.NotePad/notes) 并且没有数据类型,在我的第一个意图过滤器中,该 uri 与意图的 uri 匹配,它应该可以工作,但没有,任何人都可以解释为什么?
谢谢。