简体   繁体   English

以编程方式重置 Android 出厂设置

[英]Android Factory Reset Programmatically

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.我尝试使用 RecoverySystem 类在 Android 中执行出厂重置,但出现权限错误,我无法覆盖这些错误,因为它们是系统权限。 I want to know if there is another way to perform a factory reset?我想知道是否有其他方法可以执行恢复出厂设置?

Third party applications most DEFINITELY can do this.大多数第三方应用程序都可以做到这一点。

On 2.2+ devices (including latest 4.x) you would have to use the DevicePolicyManager and include permissions in the AndroidManifest.xml.在 2.2+ 设备(包括最新的 4.x)上,您必须使用 DevicePolicyManager 并在 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;

On Create determine there Android version and get handle on objects在创建时确定 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);
    }

The WipeDataReceiver class was a class to implement DeviceAdminReceiver, but did not have any overrides or code updates. WipeDataReceiver 类是一个实现 DeviceAdminReceiver 的类,但没有任何覆盖或代码更新。

    public static class WipeDataReceiver extends DeviceAdminReceiver {

    }

On Resume, initially the will have to confirm Factory Reset.在恢复时,最初必须确认恢复出厂设置。 when the Activity returns result it will perform wipeData.当 Activity 返回结果时,它将执行wipeData。 If it's Froyo or less, you can jump the stock factory reset activity.如果是 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) {

    }

}

you have to use DeviceAdministration (API 2.2 or above)您必须使用 DeviceAdministration(API 2.2 或更高版本)

DevicePolicyManager mDPM;设备策略管理器 mDPM; mDPM.wipeData(0); mDPM.wipeData(0);

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

This works on a many different devices I have tried.这适用于我尝试过的许多不同设备。 (more than 20) (超过20)

Context foreignContext = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.android.settings.MasterClear");

Intent intent = new Intent(foreignContext, yourClass);
startActivity(intent);

Android 8.0以后,如果能获得root权限,可以发送am广播触发恢复出厂过程:

am broadcast -p "android" --receiver-foreground -a android.intent.action.FACTORY_RESET

@TapanHP, You can use any ID just to handle the activity result callback. @TapanHP,您可以使用任何 ID 来处理活动结果回调。

int REQUEST_CODE_ENABLE_ADMIN =1234; int REQUEST_CODE_ENABLE_ADMIN =1234;

I suggest you send the user to the proper settings activity and let them do it themselves.我建议您将用户发送到适当的设置活动,让他们自己完成。

See this tutorial and use settings activity android.provider.Settings.ACTION_PRIVACY_SETTINGS请参阅本教程并使用设置活动android.provider.Settings.ACTION_PRIVACY_SETTINGS

It helps me它帮助到我

 private void resetEverything() {
    Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
    intent.setPackage("android");
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
    intent.putExtra(Intent.EXTRA_WIPE_ESIMS, shouldResetEsim());
    requireActivity().sendBroadcast(intent);
}

private boolean shouldResetEsim() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
            requireContext());
    return sharedPreferences.getBoolean(
            requireContext().getString(R.string.reset_esim), false);
}

create preference_keys.xml in /values/values 中创建preference_keys.xml

<resources>
<string name="reset_esim" translatable="false">reset_esim</string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM