Android Device Management Service sample development

Source: Internet
Author: User

See the document Docs/guide/topics/admin/device-admin.html section for an introduction to Android Device Administratioin.


This article demonstrates the implementation of Android Device policy Management through an example.


1. Sample Basic Operation diagram

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/76/D5/wKiom1ZdXWuh3pLzAADs8ui0CK4903.png "title=" Device-2015-11-27-181040.png "width=" 157 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:157px;height : 280px; "alt=" Wkiom1zdxwuh3plzaads8ui0ck4903.png "/>

is the main interface of this sample, which implements device management through the application, this example implements password setting management.

1.1 Activate device management by tapping the "Boot Device Manager" button

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/76/D5/wKiom1ZdXkOC_Tt0AAExp-CnAww052.png "title=" Device-2015-12-01-164401.png "width=" 157 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:157px;height : 280px; "alt=" Wkiom1zdxkoc_tt0aaexp-cnaww052.png "/>

Application with notification device management enabled, user click Activation, the application has device management capabilities.


1.2 Set the lock screen password restriction rule by clicking on the "Set Password Rule" button to use this policy to add Limit-password to the list of usage policies for device management.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/76/D5/wKioL1ZdXzHDZTq0AAC_1sgm19Y095.png "style=" width : 157px;height:280px; "title=" Device-2015-11-27-181109.png "width=" 157 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 " alt= "Wkiol1zdxzhdztq0aac_1sgm19y095.png"/>650) this.width=650; src= http://s4.51cto.com/wyfs02/M00/76/D6/ Wkiom1zdxs3zebotaaef044qb-4443.png "style=" width:157px;height:280px; "title=" Device-2015-11-27-181136.png "width= "157" height= "280" border= "0" hspace= "0" vspace= "0" alt= "wkiom1zdxs3zebotaaef044qb-4443.png"/>650) this.width=650 ; "Src=" Http://s3.51cto.com/wyfs02/M00/76/D6/wKiom1ZdXtCQrB4dAAFF2tcY6NY804.png "style=" width:157px;height:280px ; "title=" Device-2015-12-01-164441.png "width=" 157 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiom1zdxtcqrb4daaff2tcy6ny804.png "/>


The example calls the ACTION (Devicepolicymanager.action_set_new_password) that sets the new password, and after setting it, the password is required to see the screen lock screen.


1.3 By tapping the "Change Password" button, you can use the contents of the text box as the lock screen password, which can be done through the program.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/76/D6/wKiom1ZdYALAigjDAAFtasmTyCs591.png "title=" Device-2015-12-01-164517.png "width=" 157 "height=" 280 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:157px;height : 280px; "alt=" Wkiom1zdyalaigjdaaftasmtycs591.png "/>


The lock screen password can be reset in the application by calling the Devicepolicymanager Reset Password method (ResetPassword).


2. Concrete implementation

Implementing the entire example above requires:

A. The layout of the interface, depending on the circumstances of the actual application

Specific use of the B.devicepolicymanager class

C.deviceadminreceiver class of subclasses to implement the handling of specific broadcast events

The configuration of the broadcast receive class in the D.androidmanifest.xml file

C. List of device management policies required in the application

Note: This sample code uses the Butterknife framework.


2.1 Sample Program Interface (Layout/activity_device_admin.xml)

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "               android:layout_width= "Match_parent"                android:layout_height= "Match_parent"                android:background= "#f7f1f1"  android:orientation= "vertical"   android:padding= "15DP" >    <LinearLayout         android:layout_width= "Match_parent"         android:layout_height = "Wrap_content"         android:layout_gravity= "Center_horizontal"  android:orientation= "Horizontal" >        <EditText             android:id= "@+id/pwd_edittext"              android:layout_width= "0DP"              android:layout_height= "Wrap_content"  android:layout_margin= "10DP"  android:layout_weight= "3"  android:hint= "6-bit number"             android: Textcolor= "@android: Color/black"/>        <button             android:id= "@+id/modify_pwd_button"              android:layout_width= "0DP"              android:layout_height= "Wrap_content"              android:layout_margin= "10DP"  android:layout_weight= "1.5"  android:text= "Change Password"/>    </linearlayout>    <button         android:id= "@+id/pwd_rule_set_button"         android : layout_width= "match_parent"         android:layout_height= "Wrap_ Content "        android:text=" Set password Rules "/>    < Button        android:id= "@+id/device_admin_button"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android:text= "Enable device management "/></linearlayout>

2.2 Activity class and custom Deviceadminreceiver class

package secondriver.sdk.activity;import android.app.activity;import  android.app.admin.deviceadminreceiver;import android.app.admin.devicepolicymanager;import  Android.content.componentname;import android.content.context;import android.content.intent;import  android.os.Bundle;import android.widget.Button;import android.widget.EditText;import  Android.widget.toast;import butterknife. Bind;import butterknife. Butterknife;import butterknife. onclick;import secondriver.sdk.r;/** * author : secondriver * created  : 2015/11/27 */public class deviceadminactivity extends activity {      @Bind (R.id.pwd_edittext)     public EditText pwdEditText;     private DevicePolicyManager mDevicePolicyManager;     @Override     protected void oncreate(bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         setcontentview (R.layout.activity_device_ admin);         butterknife.bind (this);         mDevicePolicyManager =  (Devicepolicymanager)  getsystemservice (device_policy_ SERVICE);     }    //Click the Enable Device Management button      @OnClick ( R.id.device_admin_button)     public void onclickdevicebutton (Button button)  {        intent intent = new intent ( Devicepolicymanager.action_add_device_admin);         intent.putextra ( Devicepolicymanager.extra_device_admin, new componentname (This, mydeviceadminreceiver.class));       &nbsP; intent.putextra (devicepolicymanager.extra_add_explanation,  "boot Device Manager");         startactivityforresult (intent, 1);    }     //Click the Set Password Rule button      @OnClick (R.id.pwd_rule_set_button)     public  void onclickpwdrulesetbutton (Button button)  {         intent intent = new intent (Devicepolicymanager.action_set_new_password);         startactivityforresult (intent, 2);    }     //Click the Modify Password button      @OnClick (R.id.modify_pwd_button)      Public void onclickmodifypwdbutton (Button button)  {         string pwd = pwdedittext.gettext (). toString ();         componentname reCeiver = new componentname (This, mydeviceadminreceiver.class);         boolean active = mdevicepolicymanager.isadminactive (receiver);         if  (Active)  {             boolean rs = mdevicepolicymanager.resetpassword (pwd,  Devicepolicymanager.reset_password_require_entry);             if  (RS)  {                 toast.maketext (this,  "Password modified successfully",  toast.length_short). Show ();             } else {                 toast.maketext (this,  "Password modification failed",  Toast.LENGTH_ Short). Show ();            }        }  Else {            toast.maketext (this,  " No active admin ",  toast.length_short). Show ();        }     }     @Override     protected void  Onactivityresult (Int requestcode, int resultcode, intent data)  {         switch  (Requestcode)  {             case 1:                 if  (RESULTCODE&NBSP;==&NBSP;RESULT_OK)  {                     toast.maketext (This,   "Device Manager turned on successfully",  toAst. Length_short). Show ();                 } else {                     toast.maketext (this,  "Device Manager turned on failed",  toast.length_short). Show ();                }                 break;             case 2:                 if  (RESULTCODE&NBSP;==&NBSP;RESULT_OK)  {                      toast.maketext (this,  "Password rule set success",  toast.length_short). Show ();                 } else {                     toast.maketext (this,  " Password rule setup failed ",  toast.length_short). Show ();                 }                 break;            default:                 break;         }    }    //The custom device management broadcast receive class, You can override the methods in Deviceadminreceiver to implement specific functions     public static class  mydeviceadminreceiver extends deviceadminreceiver {         /**         *  overriding Methods          * <p>          * more         */          @Override          Public void ondisabled (context context, intent intent)  {             super.ondisabled (context, intent);             //Device Management Disable          }         @Override          Public void onenabled (context context, intent intent)  {             super.onenabled (context, intent);             //Device management enabled &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;}} 



2.3 Configuring the Device management recipient

<activity android:name= ". Activity. Deviceadminactivity "/>        <receiver android:name=". Activity. Deviceadminactivity$mydeviceadminreceiver "                   android:permission= "Android.permission.BIND_DEVICE_ADMIN" >             <meta-data android:name= " Android.app.device_admin "                        android:resource= "@xml/device_admin"/>             <intent-filter>                 <action android:name= " Android.app.action.DEVICE_ADMIN_ENABLED "/>            </intent-filter>        </receiver> 


Xml/device_admin content:

<?xml version= "1.0"  encoding= "Utf-8"? ><device-admin xmlns:android= "http// Schemas.android.com/apk/res/android ">    <uses-policies>         <!--Set password rules; control screen lock password length and allowed characters-->         <limit-password/>        <!--Monitor the number of screen lock unlock attempts If you have too many input errors, lock your phone or clear all data-->        <watch-login/>         <!--Reset the password for the screen lock; reset the new screen lock password-->         <reset-password/>        <!--forced lock screen, how and when to control screen lock screen-- >        <force-lock/>         <!--erase data; restore factory settings purge says there's data-->        <wipe-data/>         <!--banUse the camera, disable all device camera-->        <disable-camera/>         <!--encrypt data and encrypt stored application data-->         <encrypted-storage/>        <!--password expires; Force the user to change the screen lock password frequency--         <expire-password/>    </ Uses-policies></device-admin>



The sample generates the APK, which specifically features the device management.

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1718579

Android Device Management Service sample development

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.