1.android设备管理器(只有提示用户激活后,才可以使用)--DevicePolicyManager
:
使用到三个类:
DevicePolicyManager
密码定义的长度,密码是否要含大写字母,小写字母等设置密码需要满足的规范,锁定设备或是清除所有用户数据,要配合DeviceAdminReceiver来使用
方法:
1.进行锁屏操作
mDevicePolicyManager.lockNow()
2.
设置屏幕灯光变暗时间相关
long timeout = 1000L * Long.parseLong(et.getText().toString());
mDevicePolicyManager.setMaximumTimeToLock(mDeviceComponentName, timeout);
mDevicePolicyManager.wipeData(0);
3.
恢复出厂设置 mDevicePolicyManager.wipeData(0);
实现步骤:
1.在androidmanifest.xml中注册一个广播类,用于监听权限的变化
<receiver
android:name=".AdminReceiver"
android:description="@string/description"
android:label="@string/labelValue"
android:permission="android.permission.BIND_DEVICE_ADMIN"
>
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/lockourscreen"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
intent-filter>
receiver>
创建管理权限的清单:--》早res
的文件夹下创建一个xml
文件夹,再创建lockourscreen.xml
<device-admin
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
<wipe-data />
<reset-password />
<limit-password />
<watch-login />
uses-policies>
device-admin>
中间的内容是可以不要的。因为这部分是系统给的提示语,而我们在自己做程序的时候,可以输入自己想写的内容。
package com.xiaoma.www;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class AdminReceiver extends DeviceAdminReceiver {
@Override
public DevicePolicyManager getManager(Context context) {
Log.i("XiaoMaGuo", "调用了getManager()方法");
return super.getManager(context);
}
@Override
public ComponentName getWho(Context context) {
Log.i("XiaoMaGuo", "调用了getWho()方法");
return super.getWho(context);
}
/**
* 禁用
*/
@Override
public void onDisabled(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onDisabled()方法");
Toast.makeText(context, "禁用设备管理", Toast.LENGTH_SHORT).show();
super.onDisabled(context, intent);
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onDisableRequested()方法--这是一个可选的消息,警告有关禁止用户的请求");
return super.onDisableRequested(context, intent);
}
/**
* 激活
*/
@Override
public void onEnabled(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onEnabled()方法");
Toast.makeText(context, "启动设备管理", Toast.LENGTH_SHORT).show();
super.onEnabled(context, intent);
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onPasswordChanged()方法--设备管理:密码己经改变");
super.onPasswordChanged(context, intent);
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onPasswordFailed()方法");
super.onPasswordFailed(context, intent);
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onPasswordSucceeded()方法");
super.onPasswordSucceeded(context, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("XiaoMaGuo", "调用了onReceive()方法");
super.onReceive(context, intent);
}
@Override
public IBinder peekService(Context myContext, Intent service) {
Log.i("XiaoMaGuo", "调用了peekService()方法");
return super.peekService(myContext, service);
}
}
4.激活相关管理权限的操作的操作
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//权限列表
//EXTRA_DEVICE_ADMIN参数中说明了用到哪些权限,
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
//EXTRA_ADD_EXPLANATION参数为附加的说明
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "--小马坏,小马爱,小马瓜瓜怪--");
startActivityForResult(intent, 0);
package com.xiaoma.www;
import com.xiaoma.www.R;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @Title: DevicePolicyManagerDemoActivity.java
* @Package com.xiaoma.www
* @Description: 屏幕锁测试
* @author MZH
* @version V2.2
*/
public class DevicePolicyManagerDemoActivity extends Activity {
private Button startPermissionBtn;
private Button stopPermissionBtn;
private Button sysLockBtn;
private DevicePolicyManager dpm;
private ComponentName componentName ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化
init();
}
/**
* 初始化实现
*/
private void init(){
//资源声明定位
startPermissionBtn = (Button)findViewById(R.id.button1);
stopPermissionBtn = (Button)findViewById(R.id.button2);
sysLockBtn = (Button)findViewById(R.id.button3);
startPermissionBtn.setOnClickListener(listener);
stopPermissionBtn.setOnClickListener(listener);
sysLockBtn.setOnClickListener(listener);
//取得系统服务
dpm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, AdminReceiver.class);
}
/**
* 按钮监听器的实现,此处小马讲下,在实现监听的时候不要在一个分支里面写太多逻辑处理
* 分支只负责分支,不负责处理,要处理的话可以模块化,处理函数抽出去就可以了
* 小马希望大家从一开始就养成个好习惯,吼吼。。
*/
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//以下是三个按钮分别处理的事件实现
switch (v.getId()) {
case R.id.button1:
//开启设备权限
startDeviceManager();
break;
case R.id.button2:
//停止设备权限
stopDeviceManager();
break;
case R.id.button3:
//调用系统锁
sysLock();
break;
default:
break;
}
}
};
/**
* 启动设备管理权限
*/
private void startDeviceManager(){
//添加一个隐式意图,完成设备权限的添加
//这个Intent (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)跳转到 权限提醒页面
//并传递了两个参数EXTRA_DEVICE_ADMIN 、 EXTRA_ADD_EXPLANATION
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//权限列表
//EXTRA_DEVICE_ADMIN参数中说明了用到哪些权限,
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
//EXTRA_ADD_EXPLANATION参数为附加的说明
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "--小马坏,小马爱,小马瓜瓜怪--");
startActivityForResult(intent, 0);
}
/**
* 禁用设备管理权限方法实现
*/
private void stopDeviceManager(){
Log.i("XiaoMaGuo","------ unActiveManage ------");
boolean active = dpm.isAdminActive(componentName);
if (active) {
dpm.removeActiveAdmin(componentName);
}
}
/**
* 调用系统锁方法实现
*/
private void sysLock(){
boolean active = dpm.isAdminActive(componentName);
if (active) {
dpm.lockNow();
}
}
/**
* manager pwd setting
*/
/*private void updatePolicies() {
SharedPreferences prefs = getSamplePreferences(this);
final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY,
DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
final int maxFailedPw = prefs.getInt(PREF_MAX_FAILED_PW, 0);
boolean active = dpm.isAdminActive(componentName);
if (active) {
dpm.setPasswordQuality(componentName, pwQuality);
dpm.setPasswordMinimumLength(componentName, pwLength);
dpm.setMaximumFailedPasswordsForWipe(componentName, maxFailedPw);
}
} */
/**
* update pwd
*/
/*private void updatePolicies() {
SharedPreferences prefs = getSamplePreferences(this);
final int pwQuality = prefs.getInt(PREF_PASSWORD_QUALITY,
DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
final int pwLength = prefs.getInt(PREF_PASSWORD_LENGTH, 0);
final int maxFailedPw = prefs.getInt(PREF_MAX_FAILED_PW, 0);
boolean active = dpm.isAdminActive(componentName);
if (active) {
dpm.setPasswordQuality(componentName, pwQuality);
dpm.setPasswordMinimumLength(componentName, pwLength);
dpm.setMaximumFailedPasswordsForWipe(componentName, maxFailedPw);
}
}*/
}