This article describes the one-click screen lock program implemented by Android programming. Share it for your reference, as follows:
According to the author, all Android phones use the power button to manually lock the screen. When I use my mobile phone, I will definitely lock the screen manually at least 30 times within a day. If all use the power button, the service life of the power button will definitely not be long.
Although there are many desktop software now that integrates one-click lock screens, it is very unpleasant to install these software, because they will update automatically on a regular basis, or run something I don't want in the background. So I decided to write a lock screen program myself.
After searching in the Android development document, I found that starting from Android 2.2, the API contains a lockNow method (in the package), and the lock screen program can be implemented through this method.
Before formally writing code, we have two classes that need to be understood:
1、DevicePolicyManager
As the name suggests, this class functions to manage devices. Through this class, we can realize screen locking, brightness adjustment and even factory reset functions.
2、DeviceAdminReceiver
The parent class of this class is BroadcastReceiver, and its OnReceive method can perform different actions according to different actions.
The development process of this program is roughly as follows:
To use the method in DevicePolicyManager, you must first define a Component. Then start a DeviceAdminReceiver by managing this component.
Register a broadcast to listen for changes in permissions, the code is in the file:
<receiver android:name=".LockScreenAdmin" android:label="@string/app_name" android:description="@string/app_name" android:permission=".BIND_DEVICE_ADMIN"> <meta-data android:name=".device_admin" android:resource="@xml/lock_screen_admin" /> <intent-filter> <action android:name=".DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver>
Where, permission represents the permission required for this function; android:name=".device_admin" represents the jump interface of this action; and android:resource="@xml/lock_screen_admin" points to the following content:
<device-admin xmlns:andro> <uses-policies> <force-lock /> </uses-policies> </device-admin>
1. Implement a class inherited from DeviceAdminReceiver and implement the necessary methods. This class basically does not require writing code, so it is omitted here.
2. The following are the key codes.
This code is used to activate component when it is run for the first time. After activation, the component will be activated all the time. Use startActivityForResult() to call lockNow() in the onResult method to lock the screen. When it is not the first time it runs, directly call lockNow() to lock the screen.
if ((mComponentname)) { (); finish(); } else {// Run the program for the first time Intent intent = new Intent( DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); (DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentname); (DevicePolicyManager.EXTRA_ADD_EXPLANATION, "One key lock screen need to active"); startActivityForResult(intent, RESULT_ENABLE); }
At this point, the main part of the lock screen program has been finished.
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.