[Android] mobile guard device management permission lock screen, android lock screen
Device administrator Device Admin
Obtain the DevicePolicyManager object, and use getSystemService (DEVICE_POLICY_MANAGER) to configure the Device Policy Manager.
Call the lockNow () method of the DevicePolicyManager object to lock the device. A security exception is reported.
Create a new class named "MyAdmin" that inherits the DeviceAdminReceiver of the system. This is the broadcast receiver.
Register the configuration file
Add a <worker ER> node and set the name.
Set permission android: permission = "android. permission. BIND_DEVICE_ADMIN"
Add the <meta-data> metadata node and set the name android: name = "android. app. device_admin"
Set resource android: resource = "@ xml/device_admin_sample"
Create an xml folder in the res directory and create a device_admin_sample.xml file.
Add the <intent-filter> node and add the action, android. app. action. DEVICE_ADMIN_ENABLED.
An error is reported.
Open System settings, find the Device Manager, check the device administrator permission, and activate the device.
Enable administrator permissions
Get the Intent object, new Intent (DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN)
Call the Intent object putExtra () to pass data, the component to be activated,
Parameter: DevicePolicyManager. EXTRA_DEVICE_ADMIN, ComponentName component name object
Get the ComponentName object, new, parameter: context, Myadmin. class
Call the Intent object putExtra () to pass the explanatory data and persuade the user to enable it. parameters:
DevicePolicyManager. EXTRA_ADD_EXPLANATION, text
Call startActivity ()
Call the isAdminActive () method of the DevicePolicyManager object to determine whether there is administrator permission. Parameter: ComponentName object
Call the resetPassword () method of the DevicePolicyManager object to reset the password. Parameter: password, 0. Set password to "" to cancel the password.
Call the wipeData () method of the DevicePolicyManager object to clear data. Parameter: Clear SD card DevicePlocyManager. WIPE_EXTERANL_STORGE
If the value is 0, the factory settings are restored.
Uninstall Software
This installation cannot be uninstalled.
Call the removeActiveAdmin () method of the DevicePolicyManager object to clear the Administrator permission. The parameter is the ComponentName component name object and the ComponentName object is obtained. The parameter is "context" and "Myadmin. class ".
Get Intent object
Call the setAction () method of the Intent object and set the action. Parameter: android. intent. action. VIEW
Call addCategory () of the Intent object to add the type. Parameter: android. intent. category. DEFAULT
Call setData () of the Intent object to set data,
Parameter: Uri object. Obtain the Uri object Uri. parse ("package:" + getPackageName ())
Call startActivity ()
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>
package com.tsh.mylockscreen;
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 {
private DevicePolicyManager dpm;
ComponentName who;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
dpm = (DevicePolicyManager) getSystemService (DEVICE_POLICY_SERVICE);
who = new ComponentName (this, MyAdmin.class);
}
// One key lock screen
public void lockScreen (View v) {
if (dpm.isAdminActive (who)) {
dpm.lockNow ();
dpm.resetPassword ("123", 0);
} else {
Intent intent = new Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra (DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
intent.putExtra (DevicePolicyManager.EXTRA_ADD_EXPLANATION, "open it quickly");
startActivity (intent);
Toast.makeText (this, "No device management permission", 1) .show ();
}
}
// One-click uninstall
public void uninstall (View v) {
dpm.removeActiveAdmin (who);
Intent intent = new Intent ();
intent.setAction (Intent.ACTION_DELETE);
intent.addCategory ("android.intent.category.DEFAULT");
intent.setData (Uri.parse ("package:" + getPackageName ()));
startActivity (intent);
}
}