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?
-
6Manually? Why would an app *ever* need permission to perform a factory reset? – Kevin Coppock Jan 25 '11 at 21:14
-
12This sounds really dodgy. If a user wants a factory reset, they can do it themselves. Those permissions are there for a very good reason. – Jems Jan 25 '11 at 21:25
-
4Not to mention, if your app did this, it too would be gone after the factory reset anyway... – Kevin Coppock Jan 25 '11 at 21:29
-
9As a user, I've been longing for an app that was a one-click reset. This way I could reset my phone remotely in case it gets stolen, by configuring tasker to run this app under the right conditions. – Malabarba Feb 08 '11 at 05:31
-
Permissions are meant to be overridden. *That* is what they are there for. Function entails permissions. – Rondo May 02 '14 at 00:49
7 Answers
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. 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
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.
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. 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) {
}
}

- 355
- 3
- 10
-
-
-
-
1Are you serious? REQUEST_CODE_ENABLE_ADMIN is just int constant that you initialize any number to differ it from other result requests.. You can write any number to it.. but different from other request codes if you have any.. – Samir Alakbarov Jun 29 '21 at 08:14
-
1WipeDataReceiver is just custom class name that you create.. You can name it differently.. But if you dont know these.. You shouldt work with DeviceAdminPolicy becuase it is advanced level coding. – Samir Alakbarov Jun 29 '21 at 08:15
you have to use DeviceAdministration (API 2.2 or above)
DevicePolicyManager mDPM; mDPM.wipeData(0);
See: http://developer.android.com/guide/topics/admin/device-admin.html

- 71
- 1
- 1
After Android 8.0, you can send am broadcast to trigger the factory reset process if you can get root permission:
am broadcast -p "android" --receiver-foreground -a android.intent.action.FACTORY_RESET

- 39
- 2
This works on a many different devices I have tried. (more than 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);

- 9,137
- 5
- 50
- 55
-
4This should no longer work as of around Android 3.0. Use the device admin APIs to request a device wipe. – CommonsWare Aug 09 '12 at 11:11
@TapanHP, You can use any ID just to handle the activity result callback.
int REQUEST_CODE_ENABLE_ADMIN =1234;

- 41
- 10
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
<resources>
<string name="reset_esim" translatable="false">reset_esim</string>

- 21
- 3
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

- 4,117
- 23
- 25