下面列出了android.app.KeyguardManager#newKeyguardLock ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void wakeAndUnlock()
{
//获取电源管理器对象
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire(1000);
//得到键盘锁管理器对象
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
}
@Override
@SuppressWarnings("deprecation")
public void onCreate() {
KeyguardManager.KeyguardLock key;
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
//This is deprecated, but it is a simple way to disable the lockscreen in code
key = km.newKeyguardLock("IN");
key.disableKeyguard();
//Start listening for the Screen On, Screen Off, and Boot completed actions
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//Set up a receiver to listen for the Intents in this Service
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
super.onCreate();
}
private void wakeAndUnlock(boolean b) {
if (b) {
//获取电源管理器对象
powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG);
// 屏幕解锁
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
keyguardLock.reenableKeyguard();
keyguardLock.disableKeyguard(); // 解锁
//点亮屏幕
wakeLock.acquire();
} else {
//释放wakeLock,关灯
wakeLock.release();
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "ButlerService starting up...");
try {
shellBinder = new ShellButlerServiceBinder(this);
butlerApi = shellBinder.bind(5, TimeUnit.SECONDS);
locks = new CommonDeviceLocks();
locks.acquire(this);
// CommonDeviceLocks doesn't enable the Keyguard Lock on Q due to compatibility issues.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
keyguardLock = keyguardManager.newKeyguardLock("ButlerKeyguardLock");
keyguardLock.disableKeyguard();
}
accessibilityServiceWaiter = new AccessibilityServiceWaiter();
Log.d(TAG, "ButlerService startup completed...");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void wakeUpAndUnlock(Context context){
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
//获取电源管理器对象
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
//点亮屏幕
wl.acquire();
//释放
wl.release();
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
// 获取电源管理器对象
PowerManager pm = (PowerManager) HApp.context.getSystemService(Context.POWER_SERVICE);
// 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
KeyguardManager km = (KeyguardManager) HApp.context.getSystemService(Context.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unLock");
//初始化屏幕的监听
ScreenListener screenListener = new ScreenListener(HApp.context);
screenListener.begin(new ScreenListener.ScreenStateListener() {
@Override
public void onScreenOn() {
// Log.e("ScreenListener", "屏幕打开了");
}
@Override
public void onScreenOff() {
//在屏幕关闭的时候,进行锁屏,不执行的话,锁屏就失效了,因为要实现锁屏状态下也可以进行抢红包。
//Log.e("ScreenListener", "屏幕关闭了");
if (kl != null) {
kl.disableKeyguard();
kl.reenableKeyguard();
}
}
@Override
public void onUserPresent() {
//Log.e("ScreenListener", "解锁了");
}
});
}
private void wakeAndUnlock2(boolean b)
{
if(b)
{
//获取电源管理器对象
pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire();
//得到键盘锁管理器对象
km= (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
}
else
{
//锁屏
kl.reenableKeyguard();
//释放wakeLock,关灯
wl.release();
}
}
@Override
public IBinder onBind(Intent intent)
{
IBinder mIBinder = super.onBind(intent);
mBinding = true;
powerMan = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerMan.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "WakeLock");
keyMan = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
keyLock = keyMan.newKeyguardLock("KeyLock");
// #TODO 换掉这些deprecated方法,
return mIBinder;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextureView = new TextureView(this);
setContentView(mTextureView);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
/**
* 取消锁屏限制
*
* @param activity you know
*/
private static void cancelLockScreen(Activity activity) {
Boolean isUnlock = mIsUnlockArray.get(activity);
if (isUnlock != null && isUnlock) {
return;
}
KeyguardManager mKeyguardManager = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock(activity.getClass().getName());
mKeyguardLock.disableKeyguard();
mIsUnlockArray.put(activity, true);
}
/** Start an activity. Returns true if successful. */
@SuppressWarnings("deprecation")
protected boolean startActivity(Intent intent) {
Context context = getContext();
if (null == context || null == intent) return false;
// Disable the Keyguard if necessary.
KeyguardManager mKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (mKM.isKeyguardLocked() && !mKM.isKeyguardSecure()) {
mKeyguardLock = mKM.newKeyguardLock(getName());
mKeyguardLock.disableKeyguard();
}
try {
// Necessary because we're in a background Service!
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}
} catch (ActivityNotFoundException anfe) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), anfe);
} catch (SecurityException se) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), se);
Toast.makeText(context, R.string.permission_error, Toast.LENGTH_SHORT).show();
}
return false;
}
@SuppressLint({"MissingPermission", "WakelockTimeout"})
public static void wake(final Activity activity, final boolean disableKeyguard, final long lockDelayed) {
try {
// スリープ状態から起床(android.permission.WAKE_LOCKが必要)
final PowerManager.WakeLock wakelock
= ContextUtils.requireSystemService(activity, PowerManager.class)
.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "PowerHelper:disableLock");
if (lockDelayed > 0) {
wakelock.acquire(lockDelayed);
} else {
wakelock.acquire();
}
// キーガードを解除(android.permission.DISABLE_KEYGUARDが必要)
try {
final KeyguardManager keyguard = ContextUtils.requireSystemService(activity, KeyguardManager.class);
final KeyguardManager.KeyguardLock keylock = keyguard.newKeyguardLock(TAG);
keylock.disableKeyguard();
} finally {
wakelock.release();
}
// 画面がOFFにならないようにする
activity.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} catch (final Exception e) {
Log.w(TAG, e);
}
}
private void unlockScreen() {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("MyKeyguardLock");
keyguardLock.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
}
public static void noSysLocker(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context
.getApplicationContext().getSystemService(
Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("Zhaome");
keyguardLock.disableKeyguard();
}
public static void haveSysLocker(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context
.getApplicationContext().getSystemService(
Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("Zhaome");
keyguardLock.reenableKeyguard();
}
private void wakePhoneAndUnlock() {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "WakeLock");
mWakelock.acquire();//唤醒屏幕
//......
KeyguardManager mManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mKeyguardLock = mManager.newKeyguardLock("Lock");
//让键盘锁失效
mKeyguardLock.disableKeyguard();
mWakelock.release();//释放
}
public KeyguardLock(Context context, String tag) {
//获取系统服务
keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//初始化键盘锁,可以锁定或解开键盘锁
keyguardLock = keyguardManager.newKeyguardLock(tag);
}
/**
* Release the devices screen lock.
* @param context
*/
public static void releaseScreenLock(Context context){
KeyguardManager keyguardManager = (KeyguardManager) context.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
}
public KeyguardLockUtils(Context context, String tag) {
//获取系统服务
keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//初始化键盘锁,可以锁定或解开键盘锁
keyguardLock = keyguardManager.newKeyguardLock(tag);
}
public KeyguardLock(Context context, String tag) {
//获取系统服务
keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//初始化键盘锁,可以锁定或解开键盘锁
keyguardLock = keyguardManager.newKeyguardLock(tag);
}