2

ホーム画面アプリ「X」がデバイスにインストールされ、ユーザーがホームボタンを押すと、既定のホームと X アプリのどちらかを選択するための既定のアクションAndroid ダイアログが表示されます。

私の使用例は、ユーザーにプロンプ​​トを表示し続けることです。ただし、ユーザーが私のアプリをデフォルトとして選択するまで、このデフォルト アクション ダイアログを使用して、あまり頻繁ではありません。

  1. ACTION_MAINインテントでプロンプトを表示できます。
  2. 問題は、いつプロンプトを停止する必要があるかです。
    私の X アプリが現在デフォルトであることをどのように知ることができますか?
4

1 に答える 1

7

マニフェストに次の宣言があるアプリケーション 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>

このアプリケーションが「デフォルト オプション」であるかどうかを確認するには (この例では何でも)、次のようにします。

/**
 * 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);
}    
于 2012-05-30T13:44:07.460 に答える