Can't you detect the screen turning off on Android?

Asked 2 years ago, Updated 2 years ago, 35 views

I'm going to make an Android lock screen app. If you turn the screen off and on, you have to display the lock screen, so when the screen turns off, you want to display the lock screen app in advance and make the lock screen visible when the screen turns on. I want to do an event or an alarm when the screen is turned off? What should I do?

android

2022-09-21 16:21

1 Answers

There's something called Android ACTION_SCREEN_OFF that tells you to turn off the screen.

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    intentFilter.addAction(Intent.ACTION_SCREEN_ON);

    BroadcastReceiver screenOnOff = new BroadcastReceiver()
    {
        public static final String ScreenOff = "android.intent.action.SCREEN_OFF";
        public static final String ScreenOn = "android.intent.action.SCREEN_ON";

        public void onReceive(Context contex, Intent intent)
        {
            if (intent.getAction().equals(ScreenOff))
            {
                Log.e("MainActivity", "Screen Off"); 
            }
            else if (intent.getAction().equals(ScreenOn))
            {
                Log.e("MainActivity", "Screen On"); 
            }
        }
    };
registerReceiver(screenOnOff, intentFilter);

I think you can register your broadcast like this and take care of it properly on ScreenOff.


2022-09-21 16:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.