需求:当自己系统中有多个应用。并且自己要默认选择一个Launcher(这里我出现了一个bug,一直没有解决掉,就是默认完launcher之后,在那个默认界面一直显示的是我自己默认的launcher界面)
如果没有预置GMS,只需按照步骤一修改,如果预置了GMS请按照步骤一、二修改
1、修改ActivityManagerService.java的updateActivityUsageStats方法
if (mContentCaptureService != null && (event == Event.ACTIVITY_PAUSED || event == Event.ACTIVITY_RESUMED || event == Event.ACTIVITY_STOPPED || event == Event.ACTIVITY_DESTROYED)) { mContentCaptureService.notifyActivityEvent(userId, activity, event); } /*modified start for changed Launcher add by bnd mario start */ final PackageManager mPm = mContext.getPackageManager(); Intent homeIntent=new Intent(); homeIntent.addCategory(Intent.CATEGORY_HOME); homeIntent.setAction(Intent.ACTION_MAIN); homeIntent.addCategory(Intent.CATEGORY_DEFAULT); ResolveInfo info = mPm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY); //if("com.google.android.setupwizard".equals(info.activityInfo.packageName) || "android".equals(info.activityInfo.packageName)){ //if there is a default Launcher? ComponentName DefaultLauncher=new ComponentName("com.flintrehab.launcher","com.flintrehab.launcher.MainActivity"); //here set the package name and class name of default launcher. ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>(); ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities); ComponentName[]mHomeComponentSet = new ComponentName[homeActivities.size()]; for (int i = 0; i < homeActivities.size(); i++) { final ResolveInfo candidate = homeActivities.get(i); Log.d(TAG,"homeActivitie: candidate = "+candidate); final ActivityInfo activityInfo= candidate.activityInfo; ComponentName activityName = new ComponentName(activityInfo.packageName, activityInfo.name); mHomeComponentSet[i] = activityName; } IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_MAIN); mHomeFilter.addCategory(Intent.CATEGORY_HOME); mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT); List<ComponentName>Activities=new ArrayList(); mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,mHomeComponentSet, DefaultLauncher);// } /*modified end*/
2、找到PackageManagerService.java的SystemReady方法
` public void systemReady() { enforceSystemOrRoot(“Only the system can claim the system is ready”); //add by bnd_xnq start if(isFirstBoot()) { String examplePackageName = “com.flintrehab.launcher”; //String exampleActivityName = “com.android.launcher3.Launcher”; final int callingUserId = UserHandle.getCallingUserId(); PackageManager mypm = mContext.getPackageManager(); AppOpsManager mAppOpsManager = (AppOpsManager)
mContext.getSystemService(Context.APP_OPS_SERVICE); try { PackageInfo packinfo = mypm.getPackageInfo(examplePackageName, 0);
mAppOpsManager.setMode(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES,
packinfo.applicationInfo.uid, examplePackageName, AppOpsManager.MODE_ALLOWED);
//unknow source permmision mAppOpsManager.setMode(AppOpsManager.OP_GET_USAGE_STATS,
packinfo.applicationInfo.uid, examplePackageName, AppOpsManager.MODE_ALLOWED);
//usage access permiison } catch (NameNotFoundException e) { e.printStackTrace(); }}
if(isFirstBoot()){ String pkg=“com.flintrehab.launcher”; //android.util.Log.e(“xnq”,“xnq =” + xnq); final NotificationManager mgr =
mContext.getSystemService(NotificationManager.class); mgr.setNotificationPolicyAccessGranted(pkg, true);//Do not Disturb access
permision}
`