下面以設(shè)置Google Launcher2作為默認(rèn)啟動(dòng)的Launcher為例,其包名為com.android.launcher2
編輯packages/apps/Provision/src/com/android/DefaultActivity.java文件 ...... // remove this activity from the package manager. PackageManager pm = getPackageManager(); //add start Intent intent=new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(intent, 0); if(resolveInfoList != null){ int size = resolveInfoList.size(); for(int j=0;j<size;j++){ final ResolveInfo r = resolveInfoList.get(j); if(r.activityInfo.packageName.equals(this.getPackageName())){ resolveInfoList.remove(j); size -= 1; break; } } ComponentName[] set = new ComponentName[size]; ComponentName defaultLauncher=new ComponentName("com.android.launcher2", "com.android.launcher2.Launcher"); int defaultMatch=0; for(int i=0;i<size;i++){ final ResolveInfo resolveInfo = resolveInfoList.get(i); Log.d(TAG, resolveInfo.toString()); set[i] = new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name); if(defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)){ defaultMatch = resolveInfo.match; } } Log.d(TAG,"defaultMatch="+Integer.toHexString(defaultMatch)); IntentFilter filter=new IntentFilter(); filter.addAction(Intent.ACTION_MAIN); filter.addCategory(Intent.CATEGORY_HOME); filter.addCategory(Intent.CATEGORY_DEFAULT); pm.clearPackagePreferredActivities(defaultLauncher.getPackageName()); pm.addPreferredActivity(filter, defaultMatch, set, defaultLauncher); } //add end // remove this activity from the package manager. ComponentName name = new ComponentName(this, DefaultActivity.class); pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); ...... 然后修改packages/apps/Provision/AndroidManifest.xml文件,,增加以下語句: <uses-permission android:name=”android.permission.SET_PREFERRED_APPLICATIONS” /> 備注:上面所添加到代碼,,一定要放在pm.setComponentEnabledSetting這兩行語句之前,如果放在這個(gè)語句后面,,那后面的代碼就無法執(zhí)行了,,導(dǎo)致設(shè)置實(shí)效。 這里的com.android.launcher2是Google默認(rèn)的Launcher2,,可以根據(jù)自己的需要來定制,,讓系統(tǒng)進(jìn)入指定的那個(gè)Launcher。 |
|