How to identify if my app is set as Default by the

2019-04-12 20:52发布

Once my Home Screen app 'X' is installed on the device & when the user presses the Home Button, he is prompted with the Default Action Android dialog to choose between the Default Home & my X app.

My use case is to keep on prompting the user - a bit infrequently though, with this Default Action dialog till he selects my App as default.

  1. I can do the prompting with a ACTION_MAIN intent.
  2. Issue is when should i stop the prompting ?
    How would i know that my X app is the default now ?

1条回答
趁早两清
2楼-- · 2019-04-12 21:05

Let's say you have an application X, with the following declaration in its manifest

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

To check whether or not this application is the "default option" (in this examepl for anything) you simply:

/**
 * 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);
}    
查看更多
登录 后发表回答