//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
{
}