[DESCRIPTION]
当系统存在多个Launcher时,开机完成并解锁后,系统会弹出一个选择框让用户选择进入哪一个Launcher,如果不想选择,而是想直接进入某一个默认的Launcher,并且用户在设置->应用程序->管理应用程序->所有应用程序列表,找到之前设置的那个默认的Launcher,并且取消了默认值,可以重新弹出launcher选框。
[SOLUTION]
下面以设置Google Launcher2作为默认启动的Launcher为例,
其package name 为 com.android.launcher
launcher activity name 为 com.android.launcher2.Launcher
编辑packages/apps/Provision/src/com/android/DefaultActivity.java文件
......
// remove this activity from the package manager.
PackageManager pm = getPackageManager();
//add start
String examplePackageName = "com.android.launcher"; //请修改为需要设置的 package name
String exampleActivityName = "com.android.launcher2.Launcher"; //请修改为需要设置的 launcher activity name
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;){
final ResolveInfo r = resolveInfoList.get(j);
if(!r.activityInfo.packageName.equals(examplePackageName)){
resolveInfoList.remove(j);
size -= 1;
}else
{
j++;
}
}
ComponentName[] set = new ComponentName[size];
ComponentName defaultLauncher=new ComponentName(examplePackageName, exampleActivityName);
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这两行语句之前,
特别说明:
如果以上步骤可以达到目的,请忽略以下描述
如果以上步骤不能达到目的
请再修改 packageManagerService.java (framework/base/java/com/android/server/pm/)
的 findPreferredActivity 方法
将此方法的比较靠后的一段代码添加如下语句
if (!pa.mPref.sameSet(query, priority)) {
if(!(intent.getAction() != null
&& intent.getAction().equals(intent.ACTION_MAIN)
&& intent.getCategories()!=null
&& intent.getCategories().contains(intent.CATEGORY_HOME))) //添加这行
{ //添加这行
Slog.i(TAG, "Result set changed, dropping preferred activity for "
+ intent + " type " + resolvedType);
mSettings.mPreferredActivities.removeFilter(pa);
return null;
} //添加这行
Slog.i(TAG,"Result set not change, not drop preferred activity for special intent: "+intent);//添加这行
}
即可