English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes an Android programming implementation of a one-click lock screen program. Shared for everyone's reference, as follows:
According to the author's understanding, all Android smartphones use the power button to manually lock the screen. When the author uses the phone, the number of manual lock screen operations within a day is definitely not less than30 times. If all are done using the power button, then the lifespan of the power button will not be long.
Although many desktop software now integrate one-click lock screen functions, it is very annoying to have to install other components of these software. This is because they will regularly update automatically, or run some things in the background that I do not want. So I decided to write a lock screen program myself.
After searching through the android development documentation, I found that from android 2.2Initially, the API already includes a lockNow method (in the android.app.admin.DevicePolicyManager package), which can be used by the lock screen program to implement this function.
Before writing the code, there are two classes that we need to understand:
1、DevicePolicyManager
As the name implies, the role of this class is to manage the device. Through this class, we can implement functions such as screen locking, brightness adjustment, and even restoring factory settings.
2、DeviceAdminReceiver
The superclass of this class is BroadcastReceiver, and through its OnReceive method, different actions can be executed according to different Actions.
The development process of this program is roughly as follows:
In order to use the methods of DevicePolicyManager, it is first necessary to define a Component. Then, start a DeviceAdminReceiver by managing this component.
Register a broadcast to listen for permission changes, the code is in the AndroidManifest.xml file:}
<receiver android:name=".LockScreenAdmin" android:label="@string/app_name" android:description="@string/app_name" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/lock_screen_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"> /> </intent-filter> </receiver>
In which, permission indicates the permissions required for this feature; android:name="android.app.device_admin" indicates the jump interface of this action; and android:resource="@xml/lock_screen_admin"The content pointed to by the following is as follows:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <force-lock /> </uses-policies> </device-admin>
1Implement a class inheriting from DeviceAdminReceiver and implement the required methods. This class does not need to write much code and will be omitted here.
2The following is the key code.
This code is used to activate the component during the first run. After activation, this component will remain active. You can call lockNow() to lock the screen in the onResult method using startActivityForResult(). When it is not the first run, lock the screen directly by calling lockNow().
if (mDevicepolicymanager.isAdminActive(mComponentname)) { mDevicepolicymanager.lockNow(); finish(); }// First run of the program Intent intent = new Intent( DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentname); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "One key lock screen need to active" startActivityForResult(intent, RESULT_ENABLE); }
That's all for the main part of the lock screen program.
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', and 'Summary of Android Widget Usage'.
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.