Android Device Manager--devicepolicymanager

Source: Internet
Author: User

Since Android 2.2 (api=8), Android phones are system-level device management through the device management API.

This article describes how the device Management API can create a security-sensitive application by forcing device management policies by using the familiar "one-click Lock Screen" small project implementation.

The realization principle of one-button lock screen: When the lock screen button is pressed, a broadcast will be issued, and the lock screen can be implemented when the user interface receives a broadcast. The broadcast is implemented by calling the Locknow () method in the Devicepolicymanager (Device management receiver).

The lock screen needs to promote the application to the system administrator's permission, if the current application has the system administrator's privileges, call Locknow () to lock screen directly, if the application is first run does not have system administrator rights, you need to activate a user authorization interface to allow users to manually authorize (focus).

Therefore, the key point of one-click Lock screen is how to authorize.

The practice of having an application with system administrator privileges: Write the receiver of a broadcast, ask the broadcast recipient to apply for the privileges of the system administrator, let the operating system authorize the broadcast recipient (in fact, to activate the system's authorization component), to the user to activate themselves.

Implementation process:

The key classes:

1.DeviceAdminReceiver: The device Management receiver, which provides the intent actions that the system sends out. Your device management application must contain a deviceadminreceiver subclass. Represents the Device Manager on the phone.

2.DevicePolicyManager Device Administrator

The following code is written according to the process:

In the Mainactivity main class:

public class Mainactivity extends Activity {private Devicepolicymanager policymanager;private componentname    ComponentName;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         Policymanager = (Devicepolicymanager) getsystemservice (Context.device_policy_service);        ComponentName = new ComponentName (this, lockreceiver.class); if (policymanager.isadminactive (componentname)) {//Determines if the permission (Device Manager is activated) Policymanager.locknow ();//Direct Lock screen fi        Nish ();        }else{Activemanager ();//Activate Device Manager Get Permissions finish (); }} private void Activemanager () {///Use implicit intent to invoke the system method to activate the specified Device Manager Intent Intent = new Intent (device         Policymanager.action_add_device_admin);         Intent.putextra (Devicepolicymanager.extra_device_admin, componentname);         Intent.putextra (Devicepolicymanager.extra_add_explanation, "one button lock screen"); StartAcTivity (Intent); } }
In the Lockreceiver class:
public class Lockreceiver extends Deviceadminreceiver {@Override public     void OnReceive (context context, Intent Inten T) {         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 ("deactivated");         Super.ondisabled (context, intent);     } }

Define and declare your policy: Declare the selected policy set in Res/xml/lock.xml and it will be enforced by the program. If a program tries to invoke a method that does not have a corresponding policy in XML,

This will cause a *securityexception* exception at run time. If the program intends to manage other policies, then other permissions, such as ' _ Force Lock ' (Force-lock).

The following code fragment declares a password restriction policy in Res/xml/lock.xml:

<?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>

Refer to the XML policy statement in the Android manifest (manifest file):

    <!--referencing XML policy declaration--        <receiver             android:name= "Com.test.lockscreen.LockReceiver"             android: description= "@string/app_name"             android:label= "@string/app_name"             android:permission= " 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>         

Entire Android manifest (manifest file):

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.test.lockscreen "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" tr UE "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appth EME "> <activity android:name=" com.test.lockscreen.MainActivity "android:label=" @string /app_name "Android:theme=" @android: Style/theme.translucent.notitlebar ">> <intent-filter&gt                ; <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.catego Ry. LAUNCHER "/> </intent-filter> </activity> <receiver android : Name= "Com.test.locKscreen.             Lockreceiver "android:description=" @string/app_name "android:label=" @string/app_name " android:permission= "Android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name= "and                 Roid.app.device_admin "android:resource=" @xml/lock_screen "/> <intent-filter>         <action android:name= "Android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver> </application></manifest>

Activate device administrator

All right. Once the above code is deployed in the emulator, click the program icon to activate the view in the future to the user

If the user chooses "Activate", the program becomes the device administrator and can begin configuring and enforcing the policy. Running the program again will implement a one-click lock screen.

If the user chooses "cancle" it will be canceled.

Refer to Google's Api:http://developer.android.com/guide/topics/admin/device-admin.html#lock

Code DOWNLOAD Link: http://www.apkbus.com/android-177112-1-1.html

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.