简体   繁体   English

如何找到屏幕被锁定在android中

[英]How to find the screen is locked in android

For my application, I need to know that the screen is locked. 对于我的应用程序,我需要知道屏幕已被锁定。 How to check this is 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
}

But this always executing both if and else part... which flag I have to use to check the screen is locked or not? 但这总是执行if和else部分...我必须使用哪个标志来检查屏幕是否被锁定?

I'll try to answer this though the question is already old since it is unresolved and could help other googlers. 我会尝试回答这个问题,虽然这个问题已经很久了,因为它尚未解决,可以帮助其他googlers。 ;) ;)

First you must register a BroadcastReceiver for Intent.ACTION_SCREEN_OFF & Intent.ACTION_SCREEN_ON. 首先,您必须为Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON注册BroadcastReceiver。 Note that this receiver must be registered in codes and will not work when declared in the manifest. 请注意,此接收器必须在代码中注册,并且在清单中声明时不起作用。

In your broadcast receiver, when you receive Intent.ACTION_SCREEN_ON, you can check if the screen is locked by using the below codes: 在您的广播接收器中,当您收到Intent.ACTION_SCREEN_ON时,您可以使用以下代码检查屏幕是否被锁定:

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

if( myKeyManager.inKeyguardRestrictedInputMode()) {

 //screen is locked

} else {

 //screen is not locked

}

I guess you may have already found the answer, but if not (and for other developers), you can do it like this: 我想你可能已经找到了答案,但如果没有(对于其他开发人员),你可以这样做:

              PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
              boolean isScreenOn = powerManager.isScreenOn();

if (!isScreenOn) {
           //Screen is in OFF State
           //Code to power on and release lock 



                KeyguardManager km = (KeyguardManager) this
                 .getSystemService(Context.KEYGUARD_SERVICE);
               final KeyguardManager.KeyguardLock kl = km
                 .newKeyguardLock("MyKeyguardLock");
               kl.disableKeyguard();

               PowerManager pm = (PowerManager) this
                 .getSystemService(Context.POWER_SERVICE);
               WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
               wakeLock.acquire();
}

Register a broadcast receiver with action android.intent.action.ACTION_SCREEN_OFF and write your code in onReceive() method of receiver. 使用动作android.intent.action.ACTION_SCREEN_OFF注册广播接收器,并在接收器的onReceive()方法中编写代码。

If you are using an activity, onPause() will be called when the screen locked and onResume() will be called when the screen unlocked. 如果您正在使用某个活动,则在屏幕锁定时将调用onPause(),并在屏幕解锁时调用onResume()。

In your code you are checking some flags, i don't know where you will do that checking ? 在您的代码中,您正在检查一些标志,我不知道您将在哪里进行检查? is it continuous verification ? 是连续验证吗? If you are using an activity in your app, the above procedure will happen, just check it in Android Developers website. 如果您在应用中使用某项活动,则会执行上述步骤,只需在Android开发者网站上查看即可。

Here is what I did: 这是我做的:

This handles if the user has unlocked the screen, but not yet entered the home screen or the user's screen is turned off say during a call. 如果用户已解锁屏幕但尚未进入主屏幕或用户屏幕在通话期间关闭,则会处理此问题。

           if (Intent.ACTION_SCREEN_ON.equals(pIntent.getAction()) ||  
                     Intent.ACTION_USER_PRESENT.equals(pIntent.getAction())) {
                if(mListener!=null) {
                    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
                    boolean locked = km.inKeyguardRestrictedInputMode();
                    Log.v(TAG, ": Phone lock state from KEYGUARD_SERVICE: Current state:" + (locked ? "LOCKED":"UNLOCKED"));
                    mIsPhoneLocked = locked;
                }
            }

There are broadcasted intents for screen lock & unlock. 有屏幕锁定和解锁的广播意图。

Check it like : 检查如下:

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){//LOGIC Here}

Let me know! 让我知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM