Android 清除默认launcher 改为其他的launcher

Android 清除默认launcher 改为其他的launcher

        //clear the current preferred launcher
                ArrayList intentList = new ArrayList();
                ArrayList cnList = new ArrayList();
                mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
                IntentFilter dhIF;
                for(int i = 0; i < cnList.size(); i++) {
                dhIF = intentList.get(i);
                if(dhIF.hasAction(Intent.ACTION_MAIN) && dhIF.hasCategory(Intent.CATEGORY_HOME)) {
                mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
                //清除原有的默认launcher
                }

      // get all components and the best match
     IntentFilter filter = new IntentFilter();
     filter.addAction(Intent.ACTION_MAIN);
     filter.addCategory(Intent.CATEGORY_HOME);
     filter.addCategory(Intent.CATEGORY_DEFAULT);


     final int N = list.size();
     ComponentName[] set = new ComponentName[N];
     int bestMatch = 0;
     for (int i = 0; i < N; i++) {
         ResolveInfo r = list.get(i);
         set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
         if (r.activityInfo.packageName.equals(packageName)) {
             bestMatch = r.match;
             Log.d(TAG, "bestMatch: " + r.activityInfo.packageName);
         }
     }


     // add the default launcher as the preferred launcher
     ComponentName launcher = new ComponentName(packageName, className);
     mContext.getPackageManager().addPreferredActivity(filter, bestMatch, set, launcher);

你可能感兴趣的:(Android,android)