Android programming implementation of a key lock screen program detailed _android

Source: Internet
Author: User

The example of this article describes the Android programming implementation of a key lock screen program. Share to everyone for your reference, specific as follows:

As far as I know, all Android phones use the power button to manually lock the screen. When I use mobile phone, the number of manual lock screen in one day is absolutely not less than 30 times. If all use the power key to carry on, then the service life of the power key must not be long.

Although there are a lot of desktop software now integrated with a key lock screen, but to install these software must install their other components, this is very uncomfortable, because they will automatically update regularly, or in the background to run some things I do not want. So I decided to write a lock-screen program myself.

Looking through the Android development documentation, I've found that starting with Android 2.2, The API contains a Locknow method (Android.app.admin.DevicePolicyManager package), which can be implemented by the lock screen program.

Before we write the code formally, we have two classes that need to be understood:

1, Devicepolicymanager

As the name suggests, the role of this class is to manage devices. Through this class, we can achieve screen locking, brightness adjustment and even restore the factory settings and other functions.

2, Deviceadminreceiver

The parent class of this class is broadcastreceiver, and by its OnReceive method you can perform different actions depending on the action.

The process of development of this program is generally as follows:

To use the method in Devicepolicymanager, you first define a component. Then you can start a deviceadminreceiver by managing this component.
Register for a broadcast that listens for changes in permissions, code in the Androidmenifest.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>

Where permission represents the required permissions for this feature, Android:name= "Android.app.device_admin" represents the jump interface for this action, and android:resource= "@xml/lock_screen _admin "refers to the following:

<device-admin
 xmlns:android= "http://schemas.android.com/apk/res/android" >
  <uses-policies >
    <force-lock/>
  </uses-policies>
</device-admin>

1. Implement a class that inherits from Deviceadminreceiver and implement the necessary methods. This class basically does not have to write the code, here skips the table.

2, the following is the key code.

This code is used to activate component The first time it is run, and once activated, the component will always be active. Using Startactivityforresult (), you can call Locknow () in the Onresult method to lock the screen, and call the Locknow () lock screen directly when it is not the first time it is run.

if (mdevicepolicymanager.isadminactive (Mcomponentname)) {
  mdevicepolicymanager.locknow ();
  Finish ();
} else {//First Run 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);
}

So far, the main part of the lock screen program is finished.

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.