Android屏幕保护KeyguardManager
Android手机一般不用时,都会通过电源键来锁定屏幕同时关闭屏幕灯。
锁定效果:
要让自己的应用实现该屏幕锁定,主要需要用到以下几个类:DevicePolicyManager
这是设备管理的主类。通过它可以实现屏幕锁定、屏幕亮度调节、出厂设置等功能。
DeviceAdminReceiver
该类继承自 BroadcastReceiver 。 从源码可以看到,其实就是实现了一个OnReceive方法,该方法中根据不同的Action,执行相应的操作。 比如,如果激活成功,那么Action就是ACTION_DEVICE_ADMIN_ENABLED, 据此调用 onEnabled 方法。
系统源码:
/**
* Intercept standard device administrator broadcasts. Implementations
* should not override this method; it is better to implement the
* convenience callbacks for each action.
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PASSWORD_CHANGED.equals(action)) {
onPasswordChanged(context, intent);
} else if (ACTION_PASSWORD_FAILED.equals(action)) {
onPasswordFailed(context, intent);
} else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) {
onPasswordSucceeded(context, intent);
} else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) {
onEnabled(context, intent);
} else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) {
CharSequence res = onDisableRequested(context, intent);
if (res != null) {
Bundle extras = getResultExtras(true);
extras.putCharSequence(EXTRA_DISABLE_WARNING, res);
}
} else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) {
onDisabled(context, intent);
}
}
具体中的简单引用:
屏幕锁定和恢复出厂设置
1.创建 MyAdmin 的广播接受者 继承 DeviceAdminReceiver
<receiver android:name=".MyAdmin">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/my_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
my_admin.xml
<?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>
2.注册广播接受者为admin设备
ComponentName mAdminName = new ComponentName(this, MyAdmin.class);
if (mService != null) {
if (!mService.isAdminActive(mAdminName)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra (DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName);
startActivity(intent);
}
}
3.获取IDevicePolicyManager
public void lockScreen()
{
DevicePolicyManager dm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
ComponentName componentName = new ComponentName(this, MyDeviceAdmin.class);
boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
if(isAdminActive) //是否获得权限
{
dm.resetPassword("123", 0); //滑动解锁屏幕条后需要输入密码(方法中新设置的密码)才能真正解锁
dm.lockNow();
//dm.setMaximumTimeToLock(mDeviceComponentName, timeout); 相应时间后锁屏
//恢复出厂设置.
//dm.wipeData(0);
}else{ //获取管理员权限
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"这里可以输入一些额外的说明,比如提示用户什么的");
startActivityForResult(intent, RESULT_ENABLE_LOCKVIEW);
}
}
protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
{
super.onActivityResult(paramInt1, paramInt2, paramIntent);
DevicePolicyManager dm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
ComponentName componentName = new ComponentName(this, MyDeviceAdmin.class);
boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
if (paramInt1 == RESULT_ENABLE_LOCKVIEW && isAdminActive)
{
lockScreen();
}
else{
Toast.makeText(this, "激活失败,无法获得管理员权限", 1).show();
}
}
4.MyDeviceAdmin
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;
public class MyDeviceAdmin extends DeviceAdminReceiver
{
public static String PREF_MAX_FAILED_PW;
public static String PREF_PASSWORD_LENGTH;
public static String PREF_PASSWORD_QUALITY = "password_quality";
static
{
PREF_PASSWORD_LENGTH = "password_length";
PREF_MAX_FAILED_PW = "max_failed_pw";
}
public static SharedPreferences getDevicePreference(Context paramContext)
{
return paramContext.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0);
}
public CharSequence onDisableRequested(Context paramContext, Intent paramIntent)
{
return "这是一个可选的消息,警告有关禁止用户的请求";
}
public void onDisabled(Context paramContext, Intent paramIntent)
{
showToast(paramContext, "设备管理:不可用");
}
public void onEnabled(Context paramContext, Intent paramIntent)
{
showToast(paramContext, "设备管理:可用");
}
public void onPasswordChanged(Context paramContext, Intent paramIntent)
{
showToast(paramContext, "设备管理:密码己经改变");
}
public void onPasswordExpiring(Context paramContext, Intent paramIntent)
{
showToast(paramContext, "设备管理:密码过期");
super.onPasswordExpiring(paramContext, paramIntent);
}
public void onPasswordFailed(Context paramContext, Intent paramIntent)
{
showToast(paramContext, "设备管理:输入密码错误");
}
public void onPasswordSucceeded(Context paramContext, Intent paramIntent)
{
}
void showToast(Context paramContext, CharSequence paramCharSequence)
{
Toast.makeText(paramContext, paramCharSequence, 0).show();
}
}
<uses-permission android:name=”android.permission.RECEIVE_USER_PRESENT”/>
<uses-permission android:name=”android.permission.WAKE_LOCK”/