Android Development for lock screen function

Source: Internet
Author: User

The lock screen needs to be introduced to the device Super Administrator. Detailed instructions are available in the administration of the documentation for the Android development document. Android Device Management system functions and control access.

There are several main steps:

1 Create a broadcast receiver to implement Deviceadminreceiver

Package Com.andy.lockscreen;import android.app.admin.deviceadminreceiver;/**  * @author zhang,tianyou * @version November 20, 2014 afternoon 9:51:42 *  * Special broadcast receivers receive administrator rights broadcast */public class MyAdmin extends deviceadminreceiver{}

2 Register the broadcast in the manifest file (different ordinary broadcasts, as described in the description format):

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.andy.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=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <receiver android:name=". MyAdmin "android:description=" @string/sample_device_admin_description "Android:labeL= "@string/sample_device_admin" android:permission= "Android.permission.BIND_DEVICE_ADMIN" > <m  Eta-data android:name= "Android.app.device_admin" android:resource= "@xml/device_admin_sample" /> <intent-filter> <action android:name= "Android.app.action.DEVICE_ADMIN_ENABLED"/ > </intent-filter> </receiver> </application></manifest>

3 Create an XML folder under Res to create the corresponding XML file Device_admin_sample.xml

<device-admin xmlns:android= "Http://schemas.android.com/apk/res/android" >  <uses-policies>    <limit-password/>    <watch-login/>    <reset-password/>    <force-lock/>    < Wipe-data/>    <expire-password/>    <encrypted-storage/>    <disable-camera/>  </uses-policies></device-admin>

4 under the values file String.xml add

    <string name= "sample_device_admin_description" > User Administrator's description </string>    <string name= "Sample_ Device_admin "> Set administrative rights </string>

5 Interface Files:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.andy.lockscreen.MainActivity" > <button android:onclick= "openadmin" Android:layout_al Ignparenttop= "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:        text= "Turn on administrator rights"/> <button android:onclick= "Lockcreen" android:layout_centerinparent= "true" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "one key lock screen"/> &lt ; Button android:onclick= "Uninstall" Android:layoUt_alignparentbottom= "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Uninstall lock screen"/></relativelayout>


6 implement lock screen and turn on device administrator permissions to uninstall files

Package Com.andy.lockscreen;import Android.app.activity;import Android.app.admin.devicepolicymanager;import Android.content.componentname;import Android.content.intent;import Android.net.uri;import Android.os.Bundle; Import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {/** * device policy Service */private D Evicepolicymanager DPM; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);dp m = (Devicepolicymanager) Getsystemservice (Device_policy_service);} /** * Lock Screen * * @param view */public void Lockcreen (view view) {componentname who = new ComponentName (this, myadmin.class); /Determine if administrator privileges are turned on if (Dpm.isadminactive (WHO)) {//Lock screen Dpm.locknow ();//Set screen password The first one is the password the second is the additional parameter Dpm.resetpassword ("123", 0);// Clear Data//wipe_external_storage clear SDcard Data//0 Restore factory Settings//dpm.wipedata (devicepolicymanager.wipe_external_storage);} else {//if the prompt toast.maketext is not turned on (mainactivity.this, "Please turn on Administrator privileges first!", Toast.length_short). Show ();}} /** * Code to open Administrative rights * * @param view */public void Openadmin (view view) {//Create a Intent Add Device Administrator Intent intent = new Intent (device Policymanager.action_add_device_admin);//Activate MyAdmin broadcast receive componentname who = new ComponentName (this, myadmin.class); Intent.putextra (Devicepolicymanager.extra_device_admin, WHO);//describes the benefits of user open Administrator privileges Intent.putextra ( Devicepolicymanager.extra_add_explanation, "Open can be a button lock screen, prevent do not touch"); StartActivity (intent); Toast.maketext (mainactivity.this, "Administrator privileges are turned on!", Toast.length_short). Show (); /** * Uninstall the current software device Management data special application so can not be normal uninstall */public void Uninstall (view view) {//1. Clear Administrator Permissions first ComponentName who = new ComponentName ( This,myadmin.class);DP M.removeactiveadmin (WHO);//2. General Application Uninstall Intent Intent = new Intent () intent.setaction ("Android.intent.action.VIEW"); Intent.addcategory (" Android.intent.category.DEFAULT "); Intent.setdata (Uri.parse (" Package: "+getpackagename ())); StartActivity (Intent) ;}}



Android Development for lock screen function

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.