在8.0之前做过恢复出厂的demo,执行代码如下:
Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true);//清除数据
sendBroadcast(intent);
后面更新到8.0发现执行不了了,打开7.0正常执行的步骤,跟踪log提示Background execution not allowed:跟踪到源码发现在BroadcastQueue的scheduleTempWhitelistLocked方法时如下的判断影响到后面是否能成功执行恢复出厂设置
==========================================8.8==========================================================================
if (!skip) {
final int allowed = mService.getAppStartModeLocked(
info.activityInfo.applicationInfo.uid, info.activityInfo.packageName,
info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false);
if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
// We won't allow this receiver to be launched if the app has been
// completely disabled from launches, or it was not explicitly sent
// to it and the app is in a state that should not receive it
// (depending on how getAppStartModeLocked has determined that).
if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
Slog.w(TAG, "Background execution disabled: receiving "
+ r.intent + " to "
+ component.flattenToShortString());
skip = true;
} else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
|| (r.intent.getComponent() == null
&& r.intent.getPackage() == null
&& ((r.intent.getFlags()
& Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
&& !isSignaturePerm(r.requiredPermissions))) {
mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
component.getPackageName());
Slog.w(TAG, "Background execution not allowed: receiving "
+ r.intent + " to "
+ component.flattenToShortString());
skip = true;
}
}
}
源码中的skip如果不设置为true就可以正常恢复出厂,我们可以在执行恢复出厂操作时,在注册的FLAG中增加intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)来绕过skip设置为true的情况,进而成功恢复出厂设置。
另外,在8.0Setting源码中,恢复出厂时原先的Intent.ACTION_MASTER_CLEAR替换为最新的Intent.ACTION_FACTORY_RESET;这一点注释中有做说明,但最新的ACTION_FACTORY_RESET有说明不允许第三方应用使用是否对此有影响还不知。
个人能力有效,以上观点仅供参考