9

In my android app I want to receive the notification when the device wakes up and when the device goes to sleep. Based on this I have to perform some operations. Please help.

Please note

SCREEN_ON / OFF is different. Screen might be OFF but device might still be in wake state as in case of receiving a phone call. When we place the phone against our ear prximity sensor turns off the screen, but the device does not go to sleep.

Ankit
  • 4,426
  • 7
  • 45
  • 62

2 Answers2

2

There is inbuilt IntentFilters that you can capture.

Intent.ACTION_SCREEN_ON
Intent.ACTION_SCREEN_OFF

Using service and broadcastreceiver combination you can achieve that you looking for.

You will find complete demo HERE

UPDATE:

You can use some methods of PowerManager class.

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);

if(pm.isScreenOn()){
   // not sleep
}else{
  // sleep
}

API level >=20

if(pm.isInteractive()){
   // not sleep
}else{
  // sleep
}

Explanation :

public boolean isScreenOn ()

Added in API level 7 This method was deprecated in API level 20. Use isInteractive() instead.

Returns true if the device is in an interactive state.

For historical reasons, the name of this method refers to the power state of the screen but it actually describes the overall interactive state of the device. This method has been replaced by isInteractive().

The value returned by this method only indicates whether the device is in an interactive state which may have nothing to do with the screen being on or off. To determine the actual state of the screen, use getState().

Returns True if the device is in an interactive state.

Reference HERE

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • 1
    SCREEN_ON / OFF is different. Screen might be OFF but device might still be in wake state as in case of receiving a phone call. When we place the phone against our ear prximity sensor turns off the screen, but the device does not go to sleep. – Ankit Aug 06 '14 at 06:04
0

http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115

I think this link solves your question

  import android.content.Context;
  import android.content.Intent;

public class AEScreenOnOffReceiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

        //Toast.makeText(context, "BroadcastReceiver", Toast.LENGTH_SHORT).show();


        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            screenOff = true;

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            screenOff = false;

        }

        // Toast.makeText(context, "BroadcastReceiver :"+screenOff, Toast.LENGTH_SHORT).show();

        // Send Current screen ON/OFF value to service
        Intent i = new Intent(context, AEScreenOnOffService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18