How to check screen on or off event of android only user behavior

65 Views Asked by At

I want to show some screen on the lock screen when the screen is turned on. So I wrote the following code.

public class BroadcastReceiverScreen extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction() != null && 
       Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {

    } else if (intent.getAction() != null && 
      Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
    }
}

However, when the user does not turn on the screen with the power button, the alarm clock, message, etc are automatically displayed when the screen is turned on.

I want to make it work only when the user turns on the screen.

For example, when the phone rings, you can detect and avoid the following code.

    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_IDLE) {
                isPhoneIdle = true;
                //Log.e("PhoneCallState", "STATE_IDLE");
            } else if (state == TelephonyManager.CALL_STATE_RINGING) {
                isPhoneIdle = false;
                //Log.e("PhoneCallState", "STATE_RINGING : Incoming number " 
               + incomingNumber);
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //Log.e("PhoneCallState", "STATE_OFFHOOK");
                isPhoneIdle = false;
            }
        }
    };

In conclusion, can the user detect it only when the screen is turned on? Not automatically turned on screen.

Thank you.

0

There are 0 best solutions below