如何发现屏幕被锁定在安卓 [英] How to find the screen is locked in android

查看:125
本文介绍了如何发现屏幕被锁定在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的应用程序,我需要知道屏幕被锁定。如何成问题检查。我用下面的标记:

For my application, I need to know that the screen is locked. How to check this in problematically. I used following flag:

if(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON != 0){
    // some code
}else if((WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)!= 0){
   // some code
}

但是,这总是既执行if和else部分......我必须使用来检查屏幕被锁定或没有?这标志

But this always executing both if and else part... which flag I have to use to check the screen is locked or not?

推荐答案

我会尽力回答这个虽然这个问题已经是老,因为它是未得到解决,帮助其他谷歌。 ;)

I'll try to answer this though the question is already old since it is unresolved and could help other googlers. ;)

首先,你必须注册一个BroadcastReceiver的Intent.ACTION_SCREEN_OFF和放大器; Intent.ACTION_SCREEN_ON。请注意,此接收器必须在codeS注册,还可以在清单中声明将无法工作。

First you must register a BroadcastReceiver for Intent.ACTION_SCREEN_OFF & Intent.ACTION_SCREEN_ON. Note that this receiver must be registered in codes and will not work when declared in the manifest.

在你的广播接收器,当您收到Intent.ACTION_SCREEN_ON,您可以检查屏幕是使用下面的codeS锁定:

In your broadcast receiver, when you receive Intent.ACTION_SCREEN_ON, you can check if the screen is locked by using the below codes:

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();

这篇关于如何发现屏幕被锁定在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆