Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register to sleep event in Android?

Tags:

android

I am working on Android 3.0 and I need to know in my application when the device goes on sleep / turn off screen.

How can I register to this intent/event so I will be able to run some actions when this happens? Is there any action in BroadcastReceiver that notifies for this?

like image 681
Yaniv Avatar asked Jul 05 '11 11:07

Yaniv


People also ask

How do you put an Android app to sleep?

Navigate to and open Settings, and then tap Battery and device care. Tap Battery, and then tap Background usage limits. The following settings will be available: Put unused apps to sleep: If you haven't used an app in a while, it will automatically be put to sleep.

How do I prevent an Android device from going to sleep programmatically?

Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON . You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.

What is Android sleep mode?

With Bedtime mode, formerly known as Wind Down in the Digital Wellbeing settings, your Android phone can stay dark and quiet while you sleep. While Bedtime mode is on, it uses Do Not Disturb to silence calls, texts and other notifications that might disturb your sleep.


1 Answers

This page has a tutorial on exactly what you're looking for.

Code copied from that page (in order to turn this from a link-only answer to something directly useful):

1) Create a class in your application to receive the intent. For example, the following receiver stands alone and sets a static variable to be used in part 2:

public class ScreenReceiver extends BroadcastReceiver {
    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            wasScreenOn = true;
        }
    }
}

2) Modify your activity to receive screen on/off events. Your receiver will check the static variable in your broadcast receiver to know the reason for the intent you just received:

public class ExampleActivity extends Activity {
   @Override
    protected void onCreate() {
        // initialize receiver
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
        // your code
    }

    @Override
    protected void onPause() {
        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            System.out.println("SCREEN TURNED OFF");
        } else {
            // this is when onPause() is called when the screen state has not changed
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        // only when screen turns on
        if (!ScreenReceiver.wasScreenOn) {
            // this is when onResume() is called due to a screen state change
            System.out.println("SCREEN TURNED ON");
        } else {
            // this is when onResume() is called when the screen state has not changed
        }
        super.onResume();
    }
}
like image 52
mah Avatar answered Oct 04 '22 03:10

mah