1. Customize a class to inherit Device
public class CustomAdmin extends DeviceAdminReceiver { }
2. Create a custom_admin.xml file in the res/xml folder
<?xml version="1.0" encoding="utf-8"?> <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <limit-password /> <watch-login /> <reset-password /> <force-lock /> <wipe-data /> </uses-policies> </device-admin>
3. Register the receiver in the MainFest file
<receiver android:name="com.example.administrator.emmapplication.receiver.CustomAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/custom_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver>
4. Restore factory settings
public static void resetFactorySettings(Context context){ DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName componentName = new ComponentName(context, MyAdmin.class); boolean isAdminActive = devicePolicyManager.isAdminActive(componentName); if(isAdminActive){ devicePolicyManager.lockNow(); devicePolicyManager.wipeData(0); }else { Intent intent = new Intent(); intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); context.startActivity(intent); } }
Original address:
Android 6.0 Reset restore factory settings process analysis
https://blog.csdn.net/otaku_627/article/details/53899346
DevicePolicyManager lock screen, reset password, restore factory settings
https://blog.csdn.net/qq_29634351/article/details/70171786
Android factory reset and system upgrade process
https://blog.csdn.net/mirkerson/article/details/24322229
android factory reset process
Cnblogs.com/Free-Thinker/p/8861917.html
Android factory reset code process
https://blog.csdn.net/u010164190/article/details/54908620?locationNum=13&fps=1
Use of DeviceManager
https://www.jianshu.com/p/2ad877f5b000
DeviceManager activates and deactivates Device Manager, sets the unlocking method, locks the screen immediately, sets the screen to lock after N seconds, restores factory settings, and sets a password lock
https://www.jianshu.com/p/8934d47aed3b
Lollipop DevicePolicyManager Learning
https://blog.csdn.net/guiyu_1985/article/details/42778655
[Android Device Management] Use DevicePolicyManager to perform screen lock
https://blog.csdn.net/feng88724/article/details/6323544
https://developer.android.com/guide/topics/admin/device-admin
https://developer.android.com/work/dpc/security
https://developer.android.com/reference/android/app/admin/DevicePolicyManager