设备管理开发
Device Administration
1.creat the manifest
<receiver android:name=".app.DeviceAdminSample$DeviceAdminSampleReceiver"
android:label="@string/sample_device_admin"
android:description="@string/sample_device_admin_description"
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>
ps:注意:
1)权限
android:permission="android.permission.BIND_DEVICE_ADMIN"
2)资源文件:
android:resource="@xml/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>
3)action:
android:name="android.app.action.DEVICE_ADMIN_ENABLED"
重写DeviceAdminReceiver可以管理设备。
2.主要有三个类
DeviceAdminReceiver:重写相关操作
DeviceAPolicyManager:必须有一个DeviceAdminReceiver用户目前启用。DevicePolicyManager管理政策的一个或多个DeviceAdminReceiver实例
DeviceAdminInfo:该类用于为设备管理员指定元数据组件。
3.继承实现一个DeviceAdminRecevier
public class DeviceAdminSample extends DeviceAdminReceiver {
void showToast(Context context, String msg) {
String status = context.getString(R.string.admin_receiver_status, msg);
Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
}
@Override
public void onEnabled(Context context, Intent intent) {
showToast(context, context.getString(R.string.admin_receiver_status_enabled));
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return context.getString(R.string.admin_receiver_status_disable_warning);
}
@Override
public void onDisabled(Context context, Intent intent) {
showToast(context, context.getString(R.string.admin_receiver_status_disabled));
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context, context.getString(R.string.admin_receiver_status_pw_changed));
}
...
}
4.授权
1)创建一个Intent 添加设备管理员
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
2)激活广播接收者
ComponentName who = new ComponentName(this, DeviceAdminSample.class);
3)授权设备管理员
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
4)开启广播
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
5.devicepolicyManager
ps:ComponentName(组件名称)是用来打开其他应用程序中的Activity或服务的
类似:inflater
方法:
当前最大测试次数,当超过次数就清除数据
getMaximumFailedPasswordsForWipe (ComponentName admin)
//删除用户管理员权限
removeActiveAdmin(ComponentName admin)
//重置密码
resetPassword(String password,int flags)
//设置用户所有者的信息显示在锁屏上
setDeviceOwnerLockScreenInfo
void setDeviceOwnerLockScreenInfo (ComponentName admin,
CharSequence info)
1)获取方式:
DevicePolicyManager mDPM =
(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
2)功能
a.set password polocies 设置密码相关的
b.set device lock 设置锁屏
c.Perform data wipe 执行数据清除
3)具体操作
1.设置密码
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
2.设置密码相关的限制
属性:
PASSWORD_QUALITY_ALPHABETIC:必须至少一个字母
PASSWORD_QUALITY_ALPHANUMERIC:至少一个数字和字母
PASSWORD_QUALITY_NUMERIC:至少包含数字
PASSWORD_QUALITY_COMPLEX:至少一个数字,字母和特殊符号
PASSWORD_QUALITY_SOMETHING:无限制、
PASSWORD_QUALITY_UNSPECIFIED:无密码要求
用法:
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
...
mDPM.setPasswordQuality(mDeviceAdminSample, DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
3.设置密码内容
可以对上面的微调
setPasswordMinimumLetters()
setPasswordMinimumLowerCase()
setPasswordMinimumUpperCase()
setPasswordMinimumNonLetter()
setPasswordMinimumNumeric()
setPasswordMinimumSymbols()
setPasswordMinimumLength() 密码长度
setMaximumFailedPasswordsForWipe() 测试次数
setPasswordExpirationTimeout() 设置密码到期时间
setPasswordHistoryLength() 设置限制历史密码的数量
ex:(设置至少两个大写字母)
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
int pwMinUppercase = 2;
...
mDPM.setPasswordMinimumUpperCase(mDeviceAdminSample, pwMinUppercase);
4.设置锁屏的时间
ex:
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
...
long timeMs = 1000L*Long.parseLong(mTimeout.getText().toString());
mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs);
锁屏的方法:
DevicePolicyManager mDPM;
mDPM.lockNow();
5.初始化数据
DevicePolicyManager mDPM;
mDPM.wipeData(0); //默认一定为0
6.关闭相机
通过复选控件控制相机使用
ex:
private CheckBoxPreference mDisableCameraCheckbox;
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
...
mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked())
7.加密数据
DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
...
mDPM.setStorageEncryption(mDeviceAdminSample, true);
网友评论