Easy to achieve Android lock screen function _android

Source: Internet
Author: User
Tags myadmin

The lock screen requires the introduction of a device Super administrator. Detailed instructions are available in the administration of the document's Android development documentation. Android Device management system features and control access.

There are several main steps:

1 Create broadcast receivers, implement Deviceadminreceiver

Package com.andy.lockscreen; 
 
Import Android.app.admin.DeviceAdminReceiver; 
 
/** 
 * @author zhang,tianyou 
 * @version November 20, 2014 afternoon 9:51:42 
 * 
 * Special broadcast recipient receive Administrator privileges broadcast 
 
/public Class MyAdmin extends deviceadminreceiver{ 
 
} 

2 Register the broadcast in the manifest file (different normal broadcasts are in the descriptive 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=" true "Android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " 
    ; <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" > <meta-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> &LT;/APPLICATION&G 
 
T 
 </manifest>

3 Create an XML folder under Res, 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 admin description information </string> 
<string name= "Sample_ Device_admin > Set Administrative Permissions </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_alig Nparenttop= "true" android:layout_width= "wrap_content" android:layout_height= wrap_content "android:text=" open Administrator rights "/> <button android:onclick=" Lockcreen "android:layout_centerinparent=" true "Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "one-click Lock Screen"/> <button and roid:onclick= "Uninstall" Android:layout_alignparentbottoM= "true" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Uninstall lock screen"/>  </RelativeLayout>

6 to achieve the lock screen and open device Administrator permissions, uninstall file

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 Devicepolicymanager DPM; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre 
    Ate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
  DPM = (Devicepolicymanager) getsystemservice (Device_policy_service); /** * Lock Screen * * @param view/public void Lockcreen (view view) {componentname who = new Comp 
    Onentname (this, myadmin.class); 
      Determine if administrator permissions are turned on if (Dpm.isadminactive (WHO)) {//Lock screen Dpm.locknow (); 
 
      Set screen password The first one is the password the second one is the additional parameter Dpm.resetpassword ("123", 0);Clear Data//wipe_external_storage clear SDcard Data//0 Restore factory settings//dpm.wipedata (devicepolicymanager.wipe_exte 
    Rnal_storage); else {//If the prompt is Toast.maketext (mainactivity.this, "Please turn on admin rights First!", Toast.length_short). Show () 
    ;  }/** * Code open Admin Permissions * * @param view/public void openadmin (view view) {//Create a intent 
    Add Device Administrator Intent Intent = new Intent (devicepolicymanager.action_add_device_admin); 
 
    Activating MyAdmin broadcast receives componentname who = new ComponentName (this, myadmin.class); 
    Intent.putextra (Devicepolicymanager.extra_device_admin, WHO); 
    Describes the benefits of user-initiated administrator rights Intent.putextra (Devicepolicymanager.extra_add_explanation, "Open can be a key lock screen, prevent not touch"); 
     
    StartActivity (Intent); 
  Toast.maketext (mainactivity.this, "Administrator rights are open!", Toast.length_short). Show (); /** * Uninstall current software device Management data special application so cannot be normal uninstall/public void uninstall (view view) {//1. Clear Administrator Permissions First compo Nentname WHo = new ComponentName (this, myadmin.class); 
 
    Dpm.removeactiveadmin (WHO); 2. 
    General application of 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);  } 
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.