Question

I trying to detect if the my app is default launcher. When the launcher app is promted with Always And Just once.

Whenever I click "JUST ONCE" my app thinks it is default? Why is this happening?

I am using the following to check the default part:

private boolean isMyLauncherDefault() 
{
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);

    final String myPackageName = getPackageName();
    List<ComponentName> activities = new ArrayList<ComponentName>();
    final PackageManager packageManager = (PackageManager) getPackageManager();

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);

    for (ComponentName activity : activities) 
    {
        if (myPackageName.equals(activity.getPackageName())) 
        {
            return true;
        }
    }
    return false;
}
Was it helpful?

Solution

Use this:

private boolean isMyLauncherDefault() 
{
    boolean returnValue = false;
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.HOME");
    intent.addCategory("android.intent.category.DEFAULT");

    PackageManager pm = context.getPackageManager();
    final ResolveInfo mInfo = pm.resolveActivity(intent, 0);
    if (mInfo != null) {
         if(myAppname.equals
                     (pm.getApplicationLabel(mInfo.activityInfo.applicationInfo))
               returnValue = true;
    }
    return returnValue;
}

Edit:

getPreferredActivities:Returns the total number of registered preferred activities. Retrieve all preferred activities, previously added with addPreferredActivity(IntentFilter, int, ComponentName[], ComponentName), that are currently registered with the system. I think this will return all the activities name which are registered for that particular intent.

resolveActivity:Returns a ResolveInfo containing the final activity intent that was determined to be the best action. Returns null if no matching activity was found. So in this case we have restricted resolve activity to match DEFAULT app by specifying CATEGORY_DEFAULT.

Check this post answered by Hackborn that we should use resolveActivity() to find default app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top