This article describes the method of Android programming to implement one-click screen lock. Share it for your reference, as follows:
Here we need to use the following two classes:
DeviceAdminReceiver Device Management Component. This class provides an action that facilitates explanation of intentions issued by the system. Your device management application must contain a subclass of DeviceAdminReceiver. In this program, it represents a device manager on a mobile phone.
DevicePolicyManager A class that manages the specifications on the device. Most clients must declare a DeviceAdminReceiver that the user is currently enabled. This DevicePolicyManager manages these specifications for one or more DeviceAdminReceiver instances.
There is a method called lockNow that can directly lock the screen. However, before this, the device manager in the program needs to be activated.
Below is the main class LockActivity
package ; import ; import ; import ; import ; import ; import ; public class LockActivity extends Activity { private DevicePolicyManager policyManager; private ComponentName componentName; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); } public void LockScreen(View v){ policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); componentName = new ComponentName(this, ); if ((componentName)) {//Determine whether you have permission (the device manager is activated) ();// Directly lock the screen (()); }else{ activeManager();//Activate the device manager to obtain permission } } // Unbind public void Bind(View v){ if(componentName!=null){ (componentName); activeManager(); } } @Override protected void onResume() {//Rewrite this method to lock the screen after the device manager is activated for the first time if (policyManager!=null && (componentName)) { (); (()); } (); } private void activeManager() { //Use implicit intent to call the system method to activate the specified device manager Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); (DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); (DevicePolicyManager.EXTRA_ADD_EXPLANATION, "One-click lock screen"); startActivity(intent); } }
Below is the device manager class LockReceiver, which is a class inherited from DeviceAdminReceiver. It can receive activation/contact activation broadcasts and perform the next operation. In this program, it is just a simple print of information.
import ; import ; import ; public class LockReceiver extends DeviceAdminReceiver{ @Override public void onReceive(Context context, Intent intent) { (context, intent); ("onreceiver"); } @Override public void onEnabled(Context context, Intent intent) { ("Activate Use"); (context, intent); } @Override public void onDisabled(Context context, Intent intent) { ("Deactivate"); (context, intent); } }
Main configuration file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LockActivity" android:label="@string/app_name" android:theme="@android:style/" > <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <receiver android:name=".LockReceiver" android:description="@string/app_name" android:label="@string/app_name" android:permission=".BIND_DEVICE_ADMIN" > <meta-data android:name=".device_admin" android:resource="@xml/lock_screen" /> <intent-filter> <action android:name=".DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> </manifest>
Where lock_screen is the permission declaration of the device manager, and it needs to be defined in the form of an xml file in the res/xml directory.
<?xml version="1.0" encoding="UTF-8"?> <device-admin xmlns:andro > <uses-policies> <!-- Lock the screen --> <force-lock /> </uses-policies> </device-admin>
OK. Now you can also do one-click lock screens by yourself. You don’t have to look for all kinds of advertisements and push online.
I hope this article will be helpful to everyone's Android programming design.