下面以设置Google Launcher2作为默认启动的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这两行语句之前,如果放在这个语句后面,那后面的代码就无法执行了,导致设置实效。
这里的com.android.launcher2是Google默认的Launcher2,可以根据自己的需要来定制,让系统进入指定的那个Launcher。
编辑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这两行语句之前,如果放在这个语句后面,那后面的代码就无法执行了,导致设置实效。
这里的com.android.launcher2是Google默认的Launcher2,可以根据自己的需要来定制,让系统进入指定的那个Launcher。