控制设备管理器

  1. 启动设备管理器
  2. 停用设备管理器
  3. 检验设备管理器是否激活

设置

  1. 锁屏
  2. 设定密码输入错误次数
  3. 清除手机数据
  4. 清除手机及SD卡数据
  5. 设定锁屏幕的时间(毫秒)
  6. 设置解锁密码限制
  7. 设置密码最小长度
  8. 跳转至系统密码解锁页面

定义设备管理器广播接受者

 

 

DevicePolicyManager myDPM;
ComponentName myDeviceAdminReceiver;

// 取得DevicePolicyManager
myDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 创建MyDeviceAdminReceiver组件
myDeviceAdminReceiver = new ComponentName(this,
        MyDeviceAdminReceiver.class);

 

启动设备管理器

   Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
   intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,myDeviceAdminReceiver);
   intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"额外说明:此画面为启动设备管理器画面");
   startActivityForResult(intent, 1);

 

停用设备管理器

DevicePolicyManager myDPM = myDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

ComponentName myDeviceAdminReceiver = new ComponentName(EX05_27.this,MyDeviceAdminReceiver.class);

myDPM.removeActiveAdmin(myDeviceAdminReceiver);

 

检验设备管理器是否激活

boolean active = myDPM.isAdminActive(myDeviceAdminReceiver);

 

设置

  DevicePolicyManager myDPM;
  ComponentName myDeviceAdminReceiver;
  boolean active;

    myDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    myDeviceAdminReceiver = new ComponentName(this,
        MyDeviceAdminReceiver.class);

    active = myDPM.isAdminActive(myDeviceAdminReceiver);

 

锁屏

    if (active) {
      myDPM.lockNow();// 锁屏幕
    }

 

设定密码输入错误次数

myDPM.setMaximumFailedPasswordsForWipe(myDeviceAdminReceiver, num);

 

清除手机数据

myDPM.wipeData(0);

 

清除手机及SD卡数据

myDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);

 

设定锁屏幕的时间(毫秒)

myDPM.setMaximumTimeToLock(myDeviceAdminReceiver, timeMs);

 

设置解锁密码限制

    int myPasswordQuality[] = new int[]{
        DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, // 全部
        DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,   // 密码格式无限定
        DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,     // 数字
        DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,  // 限英文
        DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC // 限英文数字
    };
    myDPM.setPasswordQuality(myDeviceAdminReceiver, 0);

 

设置密码最小长度

// min=0代表没限制最小长度,min<4会以4为最小长度
myDPM.setPasswordMinimumLength(myDeviceAdminReceiver, min);

 

跳转至系统密码解锁页面

Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);

 

MyDeviceAdminReceiver

需要在Manifest文件中注册广播接受者,通过meta-data中的resource属性设置一个可管理的属性列表

    <receiver
        android:name="MyDeviceAdminReceiver"
        android:description="@string/device_admin_description"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"android:resource="@xml/device_admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

device_admin

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
  </uses-policies>
</device-admin>

 

public class MyDeviceAdminReceiver extends DeviceAdminReceiver{

  void showToast(Context context, CharSequence msg){
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onEnabled(Context context, Intent intent){
    showToast(context, "DeviceAdminReceiver:启动");
  }

  @Override
  public CharSequence onDisableRequested(Context context, Intent intent){
    return "DeviceAdminReceiver:是否确定停用";
  }

  @Override
  public void onDisabled(Context context, Intent intent){
    showToast(context, "DeviceAdminReceiver:停用");
  }

  @Override
  public void onPasswordChanged(Context context, Intent intent){
    showToast(context, "DeviceAdminReceiver:修改密码");
  }

  @Override
  public void onPasswordFailed(Context context, Intent intent){
    showToast(context, "DeviceAdminReceiver:密码错误");
  }

  @Override
  public void onPasswordSucceeded(Context context, Intent intent){
    showToast(context, "DeviceAdminReceiver:密码正确");
  }
}

 

posted on 2015-04-22 13:43  道无涯  阅读(932)  评论(0编辑  收藏  举报