最近给客户发样机几十台,测试同学手动操作安装了一个launcher,并设置默认launcher Always,然后重启设备发现概率性设置无效,还是会出现选择launcher弹框。但是我同样操作并没有复现问题,去和测试沟通,发现他们操作特别快,所以怀疑是否是设置后太快重启导致的设置失败,查看代码发现果真如此。下面是看到一篇别人写的文章,原文地址见文末:
-
设置默认桌面后,马上重启,有时出现无效,是因为设置后会延时10s才会将设置到结果保存到文件中。如下所示:
-
public void addPreferredActivity(IntentFilter filter, int match,
-
ComponentName[] set, ComponentName activity, int userId) {
-
addPreferredActivityInternal(filter, match, set, activity, true, userId,
-
"Adding preferred");
-
}
-
private void addPreferredActivityInternal(IntentFilter filter, int match,
-
ComponentName[] set, ComponentName activity, boolean always, int userId,
-
String opname) {
-
// writer
-
int callingUid = Binder.getCallingUid();
-
enforceCrossUserPermission(callingUid, userId, true, false, "add preferred activity");
-
if (filter.countActions() == 0) {
-
Slog.w(TAG, "Cannot set a preferred activity with no filter actions");
-
return;
-
}
-
synchronized (mPackages) {
-
if (mContext.checkCallingOrSelfPermission(
-
android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
-
!= PackageManager.PERMISSION_GRANTED) {
-
if (getUidTargetSdkVersionLockedLPr(callingUid)
-
< Build.VERSION_CODES.FROYO) {
-
Slog.w(TAG, "Ignoring addPreferredActivity() from uid "
-
+ callingUid);
-
return;
-
}
-
mContext.enforceCallingOrSelfPermission(
-
android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
-
}
-
PreferredIntentResolver pir = mSettings.editPreferredActivitiesLPw(userId);
-
Slog.i(TAG, opname + " activity " + activity.flattenToShortString() + " for user "
-
+ userId + ":");
-
filter.dump(new LogPrinter(Log.INFO, TAG), " ");
-
pir.addFilter(new PreferredActivity(filter, match, set, activity, always));
-
scheduleWritePackageRestrictionsLocked(userId);//保存到data/system/users/0/package-restrictions.xml文件中
-
}
-
}
//10s后写入问题
-
void scheduleWritePackageRestrictionsLocked(int userId) {
-
if (!sUserManager.exists(userId)) return;
-
mDirtyUsers.add(userId);
-
if (!mHandler.hasMessages(WRITE_PACKAGE_RESTRICTIONS)) {
-
mHandler.sendEmptyMessageDelayed(WRITE_PACKAGE_RESTRICTIONS, WRITE_SETTINGS_DELAY);//WRITE_SETTINGS_DELAY = 10*1000;
-
}
-
}
//写入data/system/users/0/package-restrictions.xml文件中
-
case WRITE_PACKAGE_RESTRICTIONS: {
-
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
-
synchronized (mPackages) {
-
removeMessages(WRITE_PACKAGE_RESTRICTIONS);
-
for (int userId : mDirtyUsers) {
-
mSettings.writePackageRestrictionsLPr(userId);
-
}
-
mDirtyUsers.clear();
-
}
-
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
-
} break;
原文地址:https://blog.csdn.net/u010867436/article/details/85050942