android如何获取默认的桌面程序

【方法1】

http://stackoverflow.com/questions/12594192/remove-activity-as-default-launcher/12594332#12594332

 

桌面应用的启动在INTENT中需要包含ACTION_MAIN 和CATEGORY_HOME.

通过PackageManager的resolveActivity方法来获取一个 ResolveInfo 对象来得知哪个是默认启动的Activity 

 

private void getDefaultHome() {
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        final ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
        if (res.activityInfo == null) {
            Log.d(TAG, "resolveActivity--->activityInfo null");
            // should not happen. A home is always installed, isn't it?
        } else if (res.activityInfo.packageName.equals("android")) {
            // No default selected
            Log.d(TAG, "resolveActivity--->无默认设置");
        } else {
            // res.activityInfo.packageName and res.activityInfo.name gives
            // you the default app
            Log.d(TAG, "默认桌面为:" + res.activityInfo.packageName + "."
                    + res.activityInfo.name);
        }
    }

 

 【方法2】

http://stackoverflow.com/questions/8299427/how-to-check-if-my-application-is-the-default-launcher/8361115#8361115

利用PackageManager里的getPreferredActivities()方法

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

 

posted @ 2013-07-16 09:48  随心而悦StayReal  阅读(4377)  评论(0编辑  收藏  举报