Android: Factory Reset via ADB Intent
February 2, 2022 | TechI recently needed to factory reset an Android device via ADB.
Some quick googling suggests that the command is:
adb shell am broadcast -a android.intent.action.MASTER_CLEAR
This would execute ok but nothing would occur on the device.
I could see in the logcat the following clue:
2022-02-02 12:40:35.782 3568-3604/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.MASTER_CLEAR flg=0x400010 } to android/com.android.server.MasterClearReceiver
I have encountered this issue before. It's due to the background execution limits introduced in Android O: https://developer.android.com/about/versions/oreo/background
Broadcast receivers in general are no longer allowed to register for implicit broadcasts in their manifests anymore. By implicit here, I mean that the android.intent.action.MASTER_CLEAR intent was broadcast out system wide with no specific target component specified.
To fix this, you must target the intent at the specific component that is intended to receive the intent. In this case it is the MasterClearReceiver in the Android system, as specified by the warning in the logcat.
So the correct intent is:
adb shell am broadcast -a android.intent.action.MASTER_CLEAR -n android/com.android.server.MasterClearReceiver
and indeed this worked!
I actually dug around in the master AndroidManifest.xml file and discovered more:
Looks like:
android.intent.action.MASTER_CLEAR
will eventually be deprecated in favour of:
android.intent.action.FACTORY_RESET
but for now they will both work.