The method of realizing one key lock screen with Android programming _android

Source: Internet
Author: User

An example of this article describes the Android program to achieve one-click lock Screen method. Share to everyone for your reference, specific as follows:

Here are the following two classes:

Deviceadminreceiver Device Management components. This class provides an action that facilitates the interpretation of the intent emitted by the system. Your device management application must contain a deviceadminreceiver subclass. 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 is currently enabled by a user. This Devicepolicymanager manages these specifications for one or more deviceadminreceiver instances.

An instance of Devicepolicymanager has a method called Locknow to lock the screen directly. But before that, you need to activate the Device Manager in your program.

Here is the main class lockactivity

Package com.iceman.test; 
Import android.app.Activity; 
Import Android.app.admin.DevicePolicyManager; 
Import Android.content.ComponentName; 
Import Android.content.Context; 
Import android.content.Intent; 
Import Android.os.Bundle; 
  public class Lockactivity extends activity {private Devicepolicymanager Policymanager; 
  Private ComponentName componentname; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
  Setcontentview (R.layout.main); public void Lockscreen (View v) {Policymanager = (Devicepolicymanager) getsystemservice (context.device_policy_serv 
    ICE); 
    ComponentName = new ComponentName (this, lockreceiver.class); if (policymanager.isadminactive (componentname)) {//Determine whether permission (activated Device Manager) Policymanager.locknow ()//Direct lock screen Android . os. 
    Process.killprocess (Android.os.Process.myPid ()); }else{Activemanager ()//Activate Device Manager Get Permissions}//Unbind public void bind (View v) {if (componEntname!=null) {policymanager.removeactiveadmin (componentname);
   Activemanager (); @Override protected void Onresume () {//override this method to lock the screen after the first activation of Device Manager (Policymanager!=null && POLICYM 
      Anager.isadminactive (componentname)) {Policymanager.locknow (); 
    Android.os.Process.killProcess (Android.os.Process.myPid ()); 
  } super.onresume (); private void Activemanager () {///using an implicit intent to invoke the system method to activate the specified Device Manager Intent Intent = new Intent (devicepolicymanager.act 
    Ion_add_device_admin); 
    Intent.putextra (Devicepolicymanager.extra_device_admin, componentname); 
    Intent.putextra (Devicepolicymanager.extra_add_explanation, "One key lock screen"); 
  StartActivity (Intent); 

 } 
}

The following is the Device Manager class Lockreceiver, which is a class that inherits from Deviceadminreceiver, can receive activation/contact activated broadcast, take the next step, in this program, simply print the information.

Import Android.app.admin.DeviceAdminReceiver; 
Import Android.content.Context; 
Import android.content.Intent; 
The public class Lockreceiver extends deviceadminreceiver{ 
  @Override 
  the public void onreceive (context context, Intent Intent) { 
    super.onreceive (context, intent); 
    System.out.println ("Onreceiver"); 
  } 
  @Override public 
  void onenabled (context context, Intent Intent) { 
    System.out.println ("active use"); 
    Super.onenabled (context, intent); 
  } 
  @Override public 
  void ondisabled (context context, Intent Intent) { 
    System.out.println ("deactivate"); 
    Super.ondisabled (context, intent); 
  } 
 

Main configuration file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.iceman.test "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/theme.translucent "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:na 
      Me= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver Android:name= ". Lockreceiver "android:description=" @string/app_name "android:label=" @string/app_name "Android:permiss ion= "Android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name= "AndrOid.app.device_admin "android:resource=" @xml/lock_screen "/> <intent-filter> <action Android:name= "Android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver> </ 

 Application> </manifest>

Where Lock_screen is the Device Manager's permission declaration, which needs to be defined as an XML file in the Res/xml directory

<?xml version= "1.0" encoding= "UTF-8"?> <device-admin xmlns:android= 
"http://schemas.android.com/apk/" Res/android "> 
  <uses-policies> 
    <!--lock screen--> 
    <force-lock/> 
  </ Uses-policies> 
</device-admin> 

OK. Now you can also do a key lock screen. Do not have to go to the Internet to find a variety of ads with a push.

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

Related Article

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.