问题描述
我试着使用RecoverySystem类机器人进行恢复出厂设置,但我得到许可的错误,我不能覆盖,因为它们是系统权限。我想知道是否有另一种方式来执行出厂重置?
I tried to perform a factory reset in Android using the RecoverySystem class, but I get permission errors, which I can not overwrite because they are system permissions. I want to know if there is another way to perform a factory reset?
推荐答案
第三方应用程序最肯定可以做到这一点。
Third party applications most DEFINITELY can do this.
在2.2+设备(包括最新的4.x的),你将不得不使用DevicePolicyManager,包括在AndroidManifest.xml权限。对于较旧的设备,你可以使用外资上下文加载在其他的答案中描述。
On 2.2+ devices (including latest 4.x) you would have to use the DevicePolicyManager and include permissions in the AndroidManifest.xml. For older devices you can use foreign context loader as described in other answer.
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
DevicePolicyManager mDPM;
ComponentName mDeviceAdmin;
在创建确定有Android版本,并得到处理的对象
On Create determine there Android version and get handle on objects
currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.FROYO) {
//2.2+
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdmin = new ComponentName(this, WipeDataReceiver.class);
}
在WipeDataReceiver类是一个类来实现DeviceAdminReceiver,但没有任何替代或code更新。
The WipeDataReceiver class was a class to implement DeviceAdminReceiver, but did not have any overrides or code updates.
public static class WipeDataReceiver extends DeviceAdminReceiver {
}
在继续,最初必须确认出厂设置。当活动返回结果它将执行wipeData。如果是升级Froyo或更少,你可以跳股票恢复出厂设置的活动。
On Resume, initially the will have to confirm Factory Reset. when the Activity returns result it will perform wipeData. If it's Froyo or less, you can jump the stock factory reset activity.
if (currentAPIVersion >= android.os.Build.VERSION_CODES.FROYO) {
// 2.2+
if (!mDPM.isAdminActive(mDeviceAdmin)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Process will remove user installed applications, settings, wallpaper and sound settings. Are you sure you want to wipe device?");
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
} else {
// device administrator, can do security operations
mDPM.wipeData(0);
}
} else {
// 2.1
try {
Context foreignContext = this.createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.android.settings.MasterClear");
Intent i = new Intent(foreignContext, yourClass);
this.startActivityForResult(i, REQUEST_CODE_ENABLE_ADMIN);
} catch (ClassNotFoundException e) {
}
}
这篇关于Android的出厂设置编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!