2

我正在嘗試編寫一個意圖過濾器來從聯繫人列表中選擇一個聯繫人(純粹爲了教育目的......我正在學習意圖)。在隱式意圖調用startActivity之後,android應該找到我的自定義活動以及作爲候選活動的默認活動,並讓我選擇我想使用哪一個(intent resolution)。Android意圖過濾器問題

但是,使用我目前的設置,它會打開android的默認聯繫人選擇器,而不會給我選擇。這是我的意圖過濾器。

<activity android:name=".ContactPicker" android:label="PICKER" > 
    <intent-filter> 
     <action android:name="android.intent.action.PICK" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="content" android:host="com.android.contacts" android:path="contacts" /> 
    </intent-filter> 
</activity> 

,這裏是我如何打電話的主要活動隱含的意圖:

Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://com.android.contacts/contacts")); 
startActivityForResult(intent, PICK_CONTACT); 
+0

您需要使用ACTION_CHOOSER這裏記錄的http://開發商。 android.com/reference/android/content/Intent.html#ACTION_CHOOSER - 抱歉,我不知道如何自己使用它,但嘗試谷歌搜索的例子。 – Squonk

+1

@MisterSquonk:如果用戶爲這個「Intent」選擇了一個默認處理程序,那將是必要的。可以想象,內置的聯繫人應用程序已預先確定爲默認設置,但我會感到驚訝,因爲他們不會爲我嘗試覆蓋的任何其他「內容」(例如,替代主屏幕)。 – CommonsWare

回答

4

嘗試:

<intent-filter> 
    <action android:name="android.intent.action.PICK"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:mimeType="vnd.android.cursor.dir/contact"/> 
</intent-filter> 
+0

謝謝,這工作。任何想法爲什麼通過Uri過濾意圖在這種情況下不起作用,並通過MIME類型過濾嗎? – AlephNull

+0

@AlephNull:內容提供者URI用於查找MIME類型。引用'IntentFilter'文檔,「如果你指定一個方案,但沒有類型,只有沒有類型的Intent(比如mailto :)會匹配; content:URI永遠不會匹配,因爲它們總是有一個MIME類型由他們的內容提供商提供。「通常,與MIME類型上的內容提供者過濾相關的活動。此外,在這種情況下,通過Google做的事情,您總是可以得到最好的服務。通過查看聯繫人應用程序的源代碼,我得到了上面的。 – CommonsWare

+0

太棒了,感謝您的幫助。最後一個問題,我在哪裏可以找到聯繫人應用程序的源代碼? – AlephNull