Lock screen need to introduce device Super Administrator. There are specific instructions in the administration of the documentation for the Android development document.
Android Device Management system functions and Controls 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 (for different ordinary broadcasts, in the form of a note):
<? 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:targets dkversion= "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity Android:nam E= ". 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" > <meta-data android:name= "an Droid.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 directory under Res. Create the appropriate 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 join
<string name= "Sample_device_admin_description" > User Manager Descriptive narrative information </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"/> < ; 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); /Infer If administrator privileges have been turned on if (Dpm.isadminactive (WHO)) {//Lock screen Dpm.locknow ();//Set screen password The first one is password the second is the additional number of references Dpm.resetpassword ("123", 0);//Clear Data//wipe_external_storage clear SDcard Data//0 factory reset//dpm.wipedata (Devicepolicymanager.wipe_external_ STORAGE);} else {//assume that the hint 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 Join 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 advantages of user-enabled Administrator 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