How to find the screen is locked in android

后端 未结 6 1321
猫巷女王i
猫巷女王i 2020-12-03 11:47

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         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 11:48

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

    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.

    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();
    

提交回复
热议问题