android如何獲取默認的桌面程序

【方法1】html

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

 

桌面應用的啓動在INTENT中須要包含ACTION_MAIN 和CATEGORY_HOME.web

經過PackageManager的resolveActivity方法來獲取一個 ResolveInfo 對象來得知哪一個是默認啓動的Activity app

 

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

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

利用PackageManager裏的getPreferredActivities()方法orm

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;
}
相關文章
相關標籤/搜索