以設置Google Launcher2作為默認啟動的Launcher為例,
其package name 為 com.android.launcher
launcher activity name 為 com.android.launcher2.Launcher
1、請在PackageManagerService.java中的systemReady函數的最後中增加以下代碼。
if(isFirstBoot())
{
//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);
final int callingUserId = UserHandle.getCallingUserId();
List
resolveInfoList = queryIntentActivities(intent,null, PackageManager.GET_META_DATA,callingUserId);
if(resolveInfoList != null){
int size = resolveInfoList.size();
for(int j=0;j
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
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;
}
}
Slog.e(TAG,"defaultMatch="+Integer.toHexString(defaultMatch));
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
addPreferredActivity2(filter, defaultMatch, set, defaultLauncher);
}
//MTK ADD END
}
2.在PackageManagerService.java中增加addPreferredActivity2函數。
public void addPreferredActivity2(IntentFilter filter, int match,
ComponentName[] set, ComponentName activity) {
Log.d("debug_default","addPreferredActivity2 is called.");
// writer
synchronized (mPackages) {
Slog.i(TAG, "Adding preferred activity " + activity + ":");
filter.dump(new LogPrinter(Log.INFO, TAG), " ");
mSettings.editPreferredActivitiesLPw(0).addFilter(
new PreferredActivity(filter, match, set, activity, true));
scheduleWriteSettingsLocked();
}
}
3.修改PackageManagerService.java的函數findPreferredActivity
(1) 對原來的removeMatches 增加判斷條件,在launcher時不remove
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
特別說明:
JB系列findPreferredActivity略有調整,見下面:
修改 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
}
PS:
如果用戶在設置->應用程序->管理應用程序->所有應用程序列表,找到之前設置的默認Launcher,並取消了默認值,系統會重新彈出launcher選擇框。