android 平台设置默认home后按home键弹出默认home弹框的问题

android 平台设置默认home后按home键弹出默认home弹框的问题

当时在接到修改这个bug的任务时,通过问题定位我锁定了HomeSettings.java这个文件。但是让我想不通的是android原生的代码没有问题,不应该出现设置默认home后又弹出默认home弹框。android原有设置newHome替换旧Home的方法是
mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
mHomeComponentSet, newHome.activityName);
可是进入方法内部也没有发现有什么不正常的。最终只能放弃在android自有的方法基础上去改。我重新写了一个方法加在调用它原有替换方法的后面。它原有的替换方法不能删除,否则会出现新的bug。
下面是我修改这个bug的具体方法:
path:alps/packages/apps/Settings/src/com/android/settings/HomeSettings.java
第一处修改
OnClickListener mHomeClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int index = (Integer)v.getTag();
HomeAppPreference pref = mPrefs.get(index);
if (!pref.isChecked) {
makeCurrentHome(pref);
SwitchHomescreen(getContext(),pref.activityName);//添加自定义的方法
}
}
};
//我自定义方法
private void SwitchHomescreen(Context context, ComponentName componentName) {
try {
PackageManager packageManager = context.getPackageManager();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.MAIN");
intentFilter.addCategory("android.intent.category.HOME");
intentFilter.addCategory("android.intent.category.DEFAULT");
List queryIntentActivities = packageManager.queryIntentActivities(new Intent("android.intent.action.MAIN").addCategory("android.intent.category.HOME"), 0);
ComponentName[] componentNameArr = new ComponentName[queryIntentActivities.size()];
int i = 0;
int i2 = 0;
while (i < queryIntentActivities.size()) {
ResolveInfo resolveInfo = (ResolveInfo) queryIntentActivities.get(i);
componentNameArr[i] = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
i++;
i2 = resolveInfo.match > i2 ? resolveInfo.match : i2;
}
packageManager.clearPackagePreferredActivities(componentName.getPackageName());
packageManager.addPreferredActivity(intentFilter, i2, componentNameArr, componentName);
getActivity().setResult(Activity.RESULT_OK);
} catch (Exception e) {
android.util.Log.d("lixin", "SwitchHomescreen() setDefaultLauncher occur exception" + e.getMessage());
}
}
//第二处修改
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Rebuild the list now that we might have nuked something
buildHomeActivitiesList();
// if the previous home app is now gone, fall back to the system one
if (requestCode > REQUESTING_UNINSTALL) {
// if mCurrentHome has gone null, it means we didn't find the previously-
// default home app when rebuilding the list, i.e. it was the one we
// just uninstalled. When that happens we make the system-bundled
// home app the active default.
if (mCurrentHome == null) {
for (int i = 0; i < mPrefs.size(); i++) {
HomeAppPreference pref = mPrefs.get(i);
if (pref.isSystem) {
makeCurrentHome(pref);
SwitchHomescreen(getContext(),pref.activityName);//添加自定义方法
break;
}
}
}
}
}

这样完美的解决了当已经设置好默认的Home时再按home键返回到home时出现弹框的问题。
当然如果你在开发自定义home时我自定义的方法你也可以使用。

你可能感兴趣的:(android-home键,android系统)