Android Factory重置以编程方式

Han*_*ank 17 android

我尝试使用RecoverySystem类在Android中执行工厂重置,但是我得到权限错误,我无法覆盖,因为它们是系统权限.我想知道是否有其他方法可以执行恢复出厂设置?

dma*_*cke 12

第三方应用程序最绝对可以做到这一点.

在2.2+设备(包括最新的4.x)上,您必须使用DevicePolicyManager并在AndroidManifest.xml中包含权限.对于旧设备,您可以使用外部上下文加载器,如其他答案中所述.

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;

  DevicePolicyManager mDPM;
  ComponentName mDeviceAdmin;
Run Code Online (Sandbox Code Playgroud)

On Create确定Android版本并获取对象的处理

    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);
    }
Run Code Online (Sandbox Code Playgroud)

WipeDataReceiver类是实现DeviceAdminReceiver的类,但没有任何覆盖或代码更新.

    public static class WipeDataReceiver extends DeviceAdminReceiver {

    }
Run Code Online (Sandbox Code Playgroud)

在恢复时,最初必须确认恢复出厂设置.当Activity返回结果时,它将执行wipeData.如果是Froyo或更少,您可以跳转库存恢复活动.

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) {

    }

}
Run Code Online (Sandbox Code Playgroud)


小智 7

你必须使用DeviceAdministration(API 2.2或更高版本)

DevicePolicyManager mDPM; mDPM.wipeData(0);

请参阅:http: //developer.android.com/guide/topics/admin/device-admin.html


lev*_*501 0

我建议您将用户发送到正确的设置活动并让他们自己完成。

请参阅本教程并使用设置活动android.provider.Settings.ACTION_PRIVACY_SETTINGS