SoFunction
Updated on 2025-03-01

Easily implement Android lock screen function

The lock screen requires the introduction of the device super administrator. There are detailed instructions in the Administration of the Android Development Documentation. Android device management system functions and control access.

There are mainly several steps:

1. Create a broadcast receiver and implement DeviceAdminReceiver

package ; 
 
import ; 
 
/**
  * @author Zhang,Tianyou
  * @version November 20, 2014 9:51:42 pm
  *
  * Special broadcast recipient Receive Administrator permission broadcast
  */ 
 
public class MyAdmin extends DeviceAdminReceiver{ 
 
} 

2 Register the broadcast in the manifest file (different normal broadcasts must follow the format of the instructions):

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:andro 
  package="" 
  android:versionCode="1" 
  android:versionName="1.0" > 
 
  <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 
 
  <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="" /> 
 
        <category android:name="" /> 
      </intent-filter> 
    </activity> 
     
     <receiver 
      android:name=".MyAdmin" 
      android:description="@string/sample_device_admin_description" 
      android:label="@string/sample_device_admin" 
      android:permission=".BIND_DEVICE_ADMIN" > 
      <meta-data 
        android:name=".device_admin" 
        android:resource="@xml/device_admin_sample" /> 
 
      <intent-filter> 
        <action android:name=".DEVICE_ADMIN_ENABLED" /> 
      </intent-filter> 
    </receiver> 
  </application> 
 
</manifest> 

3 Create an xml folder under res and create the corresponding xml file device_admin_sample.xml

<device-admin xmlns:andro> 
 <uses-policies> 
  <limit-password /> 
  <watch-login /> 
  <reset-password /> 
  <force-lock /> 
  <wipe-data /> 
  <expire-password /> 
  <encrypted-storage /> 
  <disable-camera /> 
 </uses-policies> 
</device-admin> 

4 Add in the values ​​file

&lt;string name="sample_device_admin_description"&gt;User administrator description information&lt;/string&gt; 
&lt;string name="sample_device_admin"&gt;Set management permissions&lt;/string&gt; 

5 interface files:

&lt;RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="" &gt; 
 
   &lt;Button 
    android:onClick="openAdmin" 
    android:layout_alignParentTop="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enable Administrator Permissions" /&gt; 
   
  &lt;Button 
    android:onClick="lockcreen" 
    android:layout_centerInParent="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="One-click lock screen" /&gt; 
 
  &lt;Button 
    android:onClick="uninstall" 
    android:layout_alignParentBottom="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Uninstall the lock screen" /&gt; 
&lt;/RelativeLayout&gt; 

6. Implement lock screen and enable device administrator permissions, uninstall files

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity { 
 
  /**
    * Equipment Policy Service
    */ 
  private DevicePolicyManager dpm; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    (savedInstanceState); 
    setContentView(.activity_main); 
    dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); 
  } 
 
  /**
    * Lock screen
    *
    * @param view
    */ 
  public void lockcreen(View view) { 
    ComponentName who = new ComponentName(this, ); 
    // Determine whether administrator permissions have been enabled    if ((who)) { 
      // Lock screen      (); 
      // Set the screen password The first one is the password The second one is the additional parameters      ("123", 0); 
 
      // Clear data      // WIPE_EXTERNAL_STORAGE Clear sdcard data      // 0 Factory reset      //(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); 
    } else { 
      // If not enabled      (, "Please enable administrator permission first!", Toast.LENGTH_SHORT) 
          .show(); 
    } 
  } 
   
 
  /**
    * Code enables management permissions
    *
    * @param view
    */ 
  public void openAdmin(View view) { 
    // Create an Intent Add Device Administrator    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 
    // Activate MyAdmin broadcast to receive    ComponentName who = new ComponentName(this, ); 
 
    (DevicePolicyManager.EXTRA_DEVICE_ADMIN, who); 
    // Explain the benefits of enabling administrator privileges by users    (DevicePolicyManager.EXTRA_ADD_EXPLANATION, 
        "Open can lock the screen with one click to prevent it from touching"); 
    startActivity(intent); 
     
    (, "Administrator permissions are enabled!", Toast.LENGTH_SHORT).show(); 
  } 
 
  /**
    * Uninstall the current software. Device management data is special. Therefore, it cannot be uninstalled normally.
    */ 
 
  public void uninstall(View view) { 
    // 1. Clear administrator permissions first    ComponentName who = new ComponentName(this, 
        ); 
    (who); 
 
    // 2. Uninstallation of ordinary applications    Intent intent = new Intent(); 
    (""); 
    (""); 
    (("package:"+getPackageName())); 
    startActivity(intent); 
 
  } 
 
} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.