一键锁屏

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值