How to distinguish the on / off status of the screen during an incoming call?

My application uses TelephonyManager.ACTION_PHONE_STATE_CHANGED for some actions. But I want another action while the phone rang when the user was present (the screen was turned on) and different actions when the user was not present (the screen was off). I tried the isScreenOn() method only at the beginning of onReceive (because when the screen is off and there is an incoming call, the screen stays for a short time). No luck - sometimes it works, sometimes not. Broadcast is asynchronous with screen state ...

 public void onReceive(Context context, Intent intent) { pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); Boolean screenOn = pm.isScreenOn(); Log.w(TAG, "Screen on is " + screenOn.toString()); 

How can I change my code to really determine if the phone was asleep when the call arrived?

+4
source share
1 answer

Ok, I got an answer that satisfies me:

 public void onReceive(Context context, Intent intent) { KeyguardManager kg = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); Boolean screenBlocked = !kg.inKeyguardRestrictedInputMode(); Log.w(TAG, "Screen lock is " + screenBlocked.toString()); 

This solution gives information if the screen is locked. And, in my case, enough - when the screen is locked during an incoming call - this means that there was no user.

+1
source

All Articles