Implement screen lock in Android Development
To lock the screen, you need to introduce the super administrator of the device. Detailed descriptions are provided in the Administration of the Android development document. Android device management system functions and controls access.
There are several steps:
1. Create a broadcast receiver to implement DeviceAdminReceiver
Package com. andy. lockscreen; import android. app. admin. deviceAdminReceiver;/*** @ author Zhang, Tianyou * @ version 9:51:42 on January 1, November 20, 2014 ** special broadcast recipient receives administrator privilege broadcast */public class MyAdmin extends DeviceAdminReceiver {}
2. register the broadcast in the configuration file (different normal broadcasts must follow the description format ):
3. Create an xml folder under res and create the corresponding xml file device_admin_sample.xml.
4. Add string. xml in the values File
User administrator description
Set Management Permissions
5. interface files:
6. Implement screen lock and enable the device administrator permission to uninstall files.
Package com. andy. lockscreen; import android. app. activity; import android. app. admin. devicePolicyManager; import android. content. componentName; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {/*** Device Policy Service */private DevicePolicyManager dpm; @ Overrideprotected void onCreate (Bundle s AvedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_main); dpm = (DevicePolicyManager) getSystemService (DEVICE_POLICY_SERVICE);}/*** lock screen ** @ param view */public void lockcreen (View view) {ComponentName who = new ComponentName (this, MyAdmin. class); // determine whether the Administrator permission is enabled if (dpm. isAdminActive (who) {// screen lock dpm. lockNow (); // set the first password on the screen to the second Is the additional parameter dpm. resetPassword ("123", 0); // clear data // WIPE_EXTERNAL_STORAGE clear sdcard data // 0 restore factory settings // dpm. wipeData (DevicePolicyManager. WIPE_EXTERNAL_STORAGE);} else {// If Toast is not enabled. makeText (MainActivity. this, "Please enable the administrator privilege first! ", Toast. LENGTH_SHORT ). show () ;}/ *** code to enable the management permission ** @ param view */public void openAdmin (View view) {// create an Intent to add the device administrator Intent = new intent (DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN); // activating MyAdmin broadcast receives ComponentName who = new ComponentName (this, MyAdmin. class); intent. putExtra (DevicePolicyManager. EXTRA_DEVICE_ADMIN, who); // indicates the intent benefit of enabling administrator permissions. putExtra (DevicePolicyManager. EXTRA_ADD_EXPLANATI ON, "enable one-click lock to prevent touch"); startActivity (intent); Toast. makeText (MainActivity. this, "the administrator privilege is enabled! ", Toast. LENGTH_SHORT ). show () ;}/ *** uninstall special applications on software device management data. Therefore, You Cannot uninstall special applications. */public void uninstall (View view) {// 1. first clear the Administrator permission ComponentName who = new ComponentName (this, MyAdmin. class); dpm. removeActiveAdmin (who); // 2. intent intent = new Intent (); intent. setAction ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. setData (Uri. parse ("package:" + getPackageName (); startActivity (intent );}}