安卓ApiDemos学习 app/Device Admin/DeviceAdminSample

首先是启动和关闭ActiveAdmin

启动ActiveAdmin

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
          "Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);

向Intent中设置DevicePolicyManager.EXTRA_DEVICE_ADMIN,本例设置了一个ComponentName

mDeviceAdminSample = new ComponentName(Controller.this, DeviceAdminSample.class);
设置EXTRA_ADD_EXPLANATION的文字会表示在权限确认画面上,如下图

安卓ApiDemos学习 app/Device Admin/DeviceAdminSample


删除ActiveAdmin

mDPM.removeActiveAdmin(mDeviceAdminSample);

设置密码相关配置。首先要确定当前activity是否已经启动了AdminActive模式

如果是的话,利用DevicePolicyManager的方法,设置密码配置

boolean active = mDPM.isAdminActive(mDeviceAdminSample);
            if (active) {
                mDPM.setPasswordQuality(mDeviceAdminSample, pwQuality);
                mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength);
                mDPM.setMaximumFailedPasswordsForWipe(mDeviceAdminSample, maxFailedPw);
            }
更改屏幕锁定password需要启动系统画面

Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
Restpassword方法中,首先判定用户是否是管理员。

if (mAM.isUserAMonkey()) {
 // Don't trust monkeys to do the right thing!
 AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this);
 builder.setMessage("You can't reset my password because you are a monkey!");
 builder.setPositiveButton("I admit defeat", null);
 builder.show();
 return;
 }
如果是,则重置密码

mDPM.resetPassword(mPassword.getText().toString(),
          DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);


你可能感兴趣的:(sample)