First, the goal
1. Realize remote lock screen and data destruction operation;
2, preliminary production "one-touch lock screen" application and optimization.
Second, the Code implementation
1. Create a new class under receiver (named Lockscreenreceiver) to inherit the Deviceadminreceiver class (which is a subclass of the Broadcastreceiver Class) and configure the receiver tag in the manifest configuration file
(1) in the receiver tag, add name, permission property, name is the full path name of the new Class (Lockscreenreceiver), permission is the necessary bind_device_admin permission;
(2) Add <meta-data> tag in receiver tag, add name, resource attribute in <meta-data> tag, name value is "Android.app.device_admin", Resource value is @xml/device_admin_lockscreen (represents the Device_admin_lockscreen.xml file in the XML folder under the Res folder, need to create a new)
(3) in the Res folder under the new folder (named XML), the XML folder under the new file and named Device_admin_lockscreen.xml, in the device_admin_lockscreen.xml copy the following code:
View Code
(4) Add the <intent-filter> tag to the Receiver tab and set its Name property value to action. device_admin_enabled;
2. In the Smsreceiver class under the Com.example.mobilesafe.receiver package, create the Device Manager (Devicepolicymanager) object (named DPM),
3. In the OnReceive method of the Smsreceiver class, inside the IF statement (else if ("#*lockscreen*#". equals) that requires a "lock screen" operation, through Getsystemservice ( String name) method instantiates the Device Manager (Devicepolicymanager) object (DPM), the parameter name is Device_policy_service, and a strong turn, and then calls its locknow () method to implement lock screen (optimization: Determine whether the application is activated in Device Manager, see step 8th);
4. Alternatively, you can invoke the Device Manager (Devicepolicymanager) object (DPM) ResetPassword (string password, int flags) to set the password after unlocking, parameter string Password is the password that needs to be set, int flags is the label, can be replaced by 0, wipedata (int flags) is used to clear the data, when the parameter is 0, the factory setting is restored, when the parameter is Devicepolicymanager.wipe_ External_storage the format of the SD card ;
5, in the phone (simulator) system settings, security-and Device Manager, select "Mobile Defender" (this information can be in the 3rd Step (1) in the receiver Tag property of the new label, description two properties to change its display), click "Activate"
(The above steps can achieve "one-click Lock Screen" application development, followed by "One-click Lock Screen" application of continued optimization)
6. Quick access to the "Device Manager" activation interface. In the 4th step, you need to find the Device Manager of your phone and manually activate it, which is cumbersome to use, so you can jump to the activation screen via code.
(1) can increase the "Activate Device Manager" button and set the Click event;
(2) Create a new ComponentName object (named Opendeviceadmin) in the Click event code and instantiate it with the new ComponentName (Context pkg, class<?> cls) method, The parameter Context pkg is a contextual, class<?> CLS for the class that needs to be activated (in this case, the class object that inherits the Deviceadminreceiver class, i.e. lockscreenreceiver.class);
(3) Create a new intent object and instantiate it with the intent (string action) method, the parameter string action (action name) is Devicepolicymanager.action_add_device_admin, The associated additional data is passed in by the Putextra (string name, parcelable value) of the intent object, with the parameter string name Devicepolicymanager.extra_device_admin, Parcelable value is the newly created ComponentName object (opendeviceadmin), and the data is passed in by Putextra (String name, parcelable value) of the intent object. The parameter string name is Devicepolicymanager.extra_add_explanation, parcelable value is the descriptive information about activating the Device Manager;
(4) Call the StartActivity (Intent Intent) method to start and jump to the activation interface.
The code is as follows;
1 New ComponentName (context, lockscreenreceiver. Class); 2 New Intent (devicepolicymanager.action_add_device_admin); 3 Intentopendeviceadmin.putextra (devicepolicymanager.extra_device_admin,opendeviceadmin); 4 Intentopendeviceadmin.putextra (Devicepolicymanager.extra_add_explanation, "about activating Device Manager description information"); 5 startactivity (intentopendeviceadmin);
View Code
7, set the Uninstall "one Button lock screen" button. Because the activated Device Manager cannot be uninstalled in the application, you can automatically uninstall it by adding the uninstall button to improve the user experience.
(1) can add "Uninstall Software" button and set the Click event;
(2) in the Click event code, first deactivate the "Device Administrator" permission to make it a normal application:
①. New ComponentName object (named Opendeviceadmin), instantiated with new componentname (context pkg, class<?> cls) method, Parameter context pkg , the class<?> CLS is the class that needs to be activated (in this case, the class object that inherits the Deviceadminreceiver class, that is, lockscreenreceiver.class);
②. Call the Device Manager (Devicepolicymanager) object (DPM) Removeactiveadmin (ComponentName who) to deactivate the class corresponding to the ComponentName object, In this example, the parameter componentname who is the ComponentName object (opendeviceadmin) instantiated in ①;
(3) Uninstall general application by intent object:
①. Create a new Intent object and instantiate it with new Intent ();
②. Add an action by intent the Setaction (string action) method of the object, and the action of the uninstaller (that is, the value of the method parameter String action) is "Android.intent.action.VIEW";
③. Increase the category by the Addcategory (String category) method of the intent object, with the parameter string category value "Android.intent.category.DEFAULT" (default);
④. Add the program path that you want to unload by using the SetData (URI data) method of the intent object, and the parameter URI data needs to turn the package full path name into a URI object through the parse (String uristring) method of the Uri object, parse () The parameter of the method string uristring value is "package:" + Application package name (which can be obtained by Getpackagename ()))
⑤. Call the StartActivity (Intent Intent) method to start the uninstaller, Intent Intent the ① object as a new Intent.
The code is as follows:
1 New Intent (); 2 Intent2.setaction ("Android.intent.action.VIEW"); 3 Intent2.addcategory ("Android.intent.category.DEFAULT"); 4 Intent2.setdata (Uri.parse ("package:" +Context.getpackagename ())); 5 startactivity (INTENT2);
View Code
8, Optimization: Before the lock screen to determine whether the app has been activated in the Device Manager to prevent program crashes
(1) Call the Device Manager (Devicepolicymanager) object (DPM) isadminactive (ComponentName Who) method to determine if the program is activated, and the parameters componentname the ① in the 7th step (2) ;
(2) According to the Isadminactive (method) of the return value of the corresponding operation, if true to execute the lock screen, if False to execute the activation method;
Android instance-Mobile security Defender (31)-according to the instructions to complete the corresponding operation two (lock screen and data removal)