2011-04-13 19 views
0

示例NotesList(由google給出,在此處查看:Note Pad)顯示瞭如何使用意圖過濾器。爲什麼NotesList中的隱式意圖無法傳遞我的活動的意圖過濾器?

在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; 
} 

此內容的細節是這樣的:

操作:空 類型:空 數據(URI):內容://com.google.provider.NotePad/notes 類別:android.intent.category.ALTERNATIVE

我希望我的ACTI被列入VITY中的菜單,和我的活動的意圖過濾器是這樣的:

<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> 

如果我改變的數據到它不work.while: < data android:mimeType="vnd.android.cursor.dir/vnd.google.note"/>,它的工作原理。

它不解我,因爲根據intents-filters

包含URI 但沒有數據類型(和類型不能 從URI推斷)一種意圖對象僅當它的URI通過測試 匹配 過濾器中的URI,同樣,過濾器 未指定類型。這將是 的情況下,僅針對像mailto:和 這樣的URI:不涉及實際數據。

在notesList的目的僅包含URI(內容://com.google.provider.NotePad/notes),並沒有數據類型,在我的第一個意圖過濾器,URI中的意圖的URI匹配的,它應該工作但不,任何人都可以解釋爲什麼?

謝謝。

回答

0

你報的右段,它說:「這將是例如mailto URI的情況只有:和電話:即不是指實際的數據。」但是content: URI肯定是指數據,所以你必須指定類型。

+0

實際上,如果我指定類型和uri在同一時間,它不會工作,而是,如果我只指定mimeType是這樣的:,它會工作,這就是爲什麼我感到困惑。 – DiveInto 2011-04-14 12:12:00

+0

實際上,對於'content:'你不需要從文檔中指定類型(http://developer.android.com/guide/components/intents-filters.html):'但是,MIME類型有時可以根據URI推斷 - 特別是當數據是content:URI時,這表明數據位於設備上並由ContentProvider控制,這使得數據MIME類型對系統可見。在內容情況下:uri該類型應該是從ContentProvider傳入的 – forcewill 2014-10-13 14:31:21