[DESCRIPTION]
开机完成并解锁后,如果系统存在多个Launcher,系统会弹出一个选择框让用户选择进入某个Launcher。如果用户不想选择,而是想直接进入某一个默认的Launcher,要怎么修改?
[SOLUTION]
下面以设置Google Launcher2作为默认启动的Launcher为例,
其package name 为 com.android.launcher
launcher activity name 为 com.android.launcher2.Launcher
1、请修改packages/apps/Provision/src/com/android/DefaultActivity.java的onCreate方法:
......
/// M: SetupWizard exist
if (!setupWizardExists) {
// Add a persistent setting to allow other apps to know the device has been provisioned.
Settings.Secure.putInt(getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 1);
}
//MTK ADD BEGIN
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);
}
//MTK 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);
......
2、请修改packages/apps/Provision/AndroidManifest.xml文件,增加以下语句:
<uses-permission android:name=”android.permission.SET_PREFERRED_APPLICATIONS” />
备注:
上面java文件中所添加的全部代码,一定要放在
ComponentName name = new ComponentName(this, DefaultActivity.class);
pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
之前
特别说明:
JB系列如果以上修改可以达到目的,请忽略以下修改。否则增加下面的内容,KK的这部分略有调整,见下面:
请再修改 packageManagerService.java (framework/base/services/java/com/android/server/pm/)的findPreferredActivity方法,将此方法比较靠后的一段代码:
if (!pa.mPref.sameSet(query, priority)) {
Slog.i(TAG, "Result set changed, dropping preferred activity for " + intent + " type " + resolvedType);
pir.removeFilter(pa);
return null;
}
修改为:
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))) { //MTK ADD
Slog.i(TAG, "Result set changed, dropping preferred activity for "+ intent + " type " + resolvedType);
pir.removeFilter(pa);
return null;
} //MTK ADD
Slog.i(TAG,"Result set not change, not drop preferred activity for special intent: "+intent);//MTK ADD
}
KK的修改方法:
前提:没有任何开机向导,然后请在上述修改defaultactivity基础上做下面的修改。如果是使用GMS包中开机向导则无法修改,如果是使用MTK的OOBE,则可以把defaultactivity的改动,搬移到OOBE的最后一个Activity的onCreate函数中即可。
也就是调整下之前PackageManagerService.java的内容,修改机制没变,只是kk多了一个地方会removefilter,去掉就可以
PackageManagerService.java
(1)对原来的removeMatches 增加判断条件,在launcher时不remove
ResolveInfo findPreferredActivity(Intent intent, String resolvedType, int flags,
if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null && intent.getCategories().contains(intent.CATEGORY_HOME))) { //MTK ADD
Slog.d(TAG,"launcher");
}else{
if (removeMatches) {
pir.removeFilter(pa);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing match " + pa.mPref.mComponent);
}
(2)
if (always && !pa.mPref.sameSet(query, priority)) {
Slog.i(TAG, "Result set changed, dropping preferred activity for "
+ intent + " type " + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing preferred activity since set changed "
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a "last chosen" entry (!always)
PreferredActivity lastChosen = new PreferredActivity(
pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
mSettings.writePackageRestrictionsLPr(userId);
return null;
}
上面这段修改为下面的内容:
if (always && !pa.mPref.sameSet(query, priority)) {
if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null &&
intent.getCategories().contains(intent.CATEGORY_HOME))) { //MTK ADD
Slog.i(TAG, "Result set changed, dropping preferred activity for "
+ intent + " type " + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing preferred activity since set changed "
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a "last chosen" entry (!always)
PreferredActivity lastChosen = new PreferredActivity(
pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
mSettings.writePackageRestrictionsLPr(userId);
return null;
}
}
Slog.i(TAG,"Result set not change, not drop preferred activity for special intent: "+intent);//MTK ADD
// Yay! Either the set matched or we're looking for the last chosen
PS:
如果用户在设置->应用程序->管理应用程序->所有应用程序列表,找到之前设置的默认Launcher,并取消了默认值,系统会重新弹出launcher选择框。