//2.2以上的版本支持一键锁屏
private static final int MINSDK_CANUSE_ONEKEYLOCK = 8;
/**
* 判断一键锁屏功能是否被激活
*
* @param context
* @return
*/
public static boolean isOneKeyLockActive(Context context) {
if (android.os.Build.VERSION.SDK_INT >= MINSDK_CANUSE_ONEKEYLOCK) {
DevicePolicyManager devicepolicymanager = (DevicePolicyManager) context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
return devicepolicymanager.isAdminActive(new ComponentName(context,
LockScreenReceiver.class));
} else {
return false;
}
}
/**
* 实施锁屏
*
* @param context
*/
public static void lockNow(Context context) {
if (android.os.Build.VERSION.SDK_INT >= MINSDK_CANUSE_ONEKEYLOCK) {
DevicePolicyManager devicepolicymanager = (DevicePolicyManager) context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
devicepolicymanager.lockNow();
}
}
/**
* 取消(反激活)一键锁屏功能
*
* @param context
*/
public static void unActiveOneKeyLock(Context context) {
if (android.os.Build.VERSION.SDK_INT >= MINSDK_CANUSE_ONEKEYLOCK) {
DevicePolicyManager devicepolicymanager = (DevicePolicyManager) context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
devicepolicymanager.removeActiveAdmin(new ComponentName(context,
LockScreenReceiver.class));
}
}
//激活一键锁屏
Intent i = new Intent("android.app.action.ADD_DEVICE_ADMIN");
i.putExtra("android.app.extra.DEVICE_ADMIN", new ComponentName(MainActivity.this,
LockScreenReceiver.class));
String text = getString(R.string.onekey_lock_tip2);
i.putExtra("android.app.extra.ADD_EXPLANATION", text);
startActivity(i);
/**
* 用于承载一键锁屏功能,不需要实现
*/
public class LockScreenReceiver extends DeviceAdminReceiver
{
}