2012-05-30 56 views
3

當用戶按下主頁按鈕後,我的主屏幕應用「X」安裝在設備&上時,系統會提示他用Android對話框在我的X應用的默認主頁&之間進行選擇。如何識別我的應用程序是否被用戶設置爲默認?

我的使用案例是繼續提示用戶 - 不太常見,但是這個默認操作對話框直到他選擇我的應用程序作爲默認值。

  1. 我可以做一個ACTION_MAIN意圖的提示。
  2. 問題是我應該何時停止提示?
    我怎麼會知道我的X應用程序現在是默認的?

回答

6

假設你有一個應用程序X,在其清單

<activity android:name=".MyActivity" 
     android:label="@string/app_name"> 
    <!-- filter: This activity can be the default view action for a row in 
     RawContacts --> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <data android:mimeType="vnd.android.cursor.item/raw_contact" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

以下聲明要檢查這個應用程序是否是(對於任何這個examepl )「默認選項」你只需:

/** 
* Returns true if the supplied component name is the preferred activity 
* for any action. 
* @param component The ComponentName of your Activity, e.g. 
* Activity#getComponentName(). 
*/ 
boolean isDefault(ComponentName component) { 
    ArrayList<ComponentName> components = new ArrayList<ComponentName>(); 
    ArrayList<IntentFilter> filters = new ArrayList<IntentFilter>(); 
    getPackageManager().getPreferredActivities(filters, components, null); 
    return components.contains(component); 
}  
+0

太棒了。正是我想要的。雖然我不能upvote答案.. – rbot

+1

完美!正是我在找的東西。我只想補充一點,你可以在Activity中調用它: isDefault(this.getComponentName()); – Ivo

+0

,如果我的應用程序不是默認的,那我該如何讓它默認 –

相關問題