List of default apps showing wrong in Android L

こ雲淡風輕ζ 提交于 2019-12-22 03:36:28

问题


I want to get all default apps in Android L. I used bellow code but they give me a wrong solution. Let see my code first

private void getMyAppLauncherDefault() {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);
    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);
    List<ComponentName> activities = new ArrayList<ComponentName>();
    final PackageManager packageManager = (PackageManager) getPackageManager();

    packageManager.getPreferredActivities(filters, activities, null);
    for (ComponentName activity : activities) {

        Log.d(TAG,"======packet default:==="+activity.getPackageName());
    }
}

And this is log. The log shows a wrong result between com.google.android.googlequicksearchbox and com.vlingo.midas. They are both Voice apps, but I set up com.google.android.googlequicksearchbox as default. I do not know why the log shows com.vlingo.midas. How can I fix it? Thanks

 16:02:44.817 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.vlingo.midas
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.launcher
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.google.android.googlequicksearchbox
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
 16:02:44.827 /com.exam D/Sample: ======packet default:===com.google.android.apps.plus

update: There are default app names


回答1:


To check if your App is set as "Default" then please try this code:

 public static boolean isMyAppDefault(Context context) {
        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 = context.getPackageName();
        List<ComponentName> activities = new ArrayList<ComponentName>();
        final PackageManager packageManager = (PackageManager) context.getPackageManager();

        packageManager.getPreferredActivities(filters, activities, null);

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



回答2:


The code you added above is perfectly right. It does perform what it is meant to.

Now you have set com.google.android.googlequicksearchbox voice app as default and that's why it's showing up in the log. While com.vlingo.midas is showing probably because it's set as default for some other kind of category instead of voice.




回答3:


I'm using this method to check the default launcher.

public void foo(){
       String defaultHomeApp = getDefaultAppByCategory(context, Intent.CATEGORY_HOME);
       ...
}

private String getDefaultAppByCategory(Context context, String category) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(category);
        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return resolveInfo.activityInfo.packageName;
    }

You can use an Intent to obtain which app the user is using as default.

String packageApp= context.getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(<ADD_HERE_THE_CATEGORY>);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

The resolveInfo variable will have the default package for that category.



来源:https://stackoverflow.com/questions/40501710/list-of-default-apps-showing-wrong-in-android-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!