RickNotFred
RickNotFred

Reputation: 3401

Is there a way to programmatically perform a factory reset on an Android device?

Understanding how incredibly dangerous this question is, I'd like to know if it is possible to programmatically issue a factory reset, as available in Droid and G1. Does anyone know how this is implemented? Is this implemented using the standard Android SDK, a Motorola-specific function, or something else?

Upvotes: 6

Views: 8806

Answers (3)

khalifa R.
khalifa R.

Reputation: 98

import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.os.Build

class DeviceWipeManager(private val ctx: Context) {
    private val dpm = ctx.getSystemService(DevicePolicyManager::class.java)
    private val deviceAdmin by lazy { ComponentName(ctx, DeviceAdminReceiver::class.java) }

    // Method to perform wipe data
    fun performWipe() {
        try {
            var flags = 0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
                flags = flags.or(DevicePolicyManager.WIPE_SILENTLY)
                //WIPE_SILENTLY
            }`enter code here`
            dpm?.wipeData(flags)
            //SUCCESS

        }catch (e : SecurityException){
            //log exception
        }
    }
}

you need to have the following permissions:

android.permission.MANAGE_DEVICE_POLICY_WIPE_DATA
android.permission.MANAGE_DEVICE_POLICY_ACROSS_USERS
android.permission.MANAGE_DEVICE_POLICY_FACTORY_RESET

I was searching to achieve the same thing, I have tried many things but finally, i have found this solution, it is working on Android 14, however, it is not working on the emulator

Upvotes: 0

Alireza
Alireza

Reputation: 465

You can use the DevicePolicyManager method wipeData() to reset the device to factory settings. This is useful if the device is lost or stolen. Often the decision to wipe the device is the result of certain conditions being met. For example, you can use setMaximumFailedPasswordsForWipe() to state that a device should be wiped after a specific number of failed password attempts.

You wipe data as follows: Java:

DevicePolicyManager dpm;
dpm.wipeData(0);

Kotlin:

private lateinit var dpm: DevicePolicyManager
dpm.wipeData(0)

Here is the site refrence

Upvotes: 2

Thomas Moller
Thomas Moller

Reputation: 121

You may use the WipeData() method in DevicePolicyManager, which will erase all user data: http://developer.android.com/guide/topics/admin/device-admin.html

Available since Android 2.2

Close enough?

Upvotes: 12

Related Questions