4

I've an activity that, on its onPause has to do some work but not when the screen is turned off. I've already registered the receiver for the ACTION_SCREEN_OFF intent and, theoretically, this and a static flag at application level should do the trick but... It doesn't work because the onPause callback on the activity is invoked BEFORE the receiver can get its Intent. That is: logcat*ting* while push down the idle button, i can see the onPause trace first and the onReceive after. At this point, setting up the static flag is not very important...

Any possibilities to know at activity's onPause time that the screen has turned off?

Thanks in advance
L.

AakashM
  • 62,551
  • 17
  • 151
  • 186
lorenzoff
  • 1,120
  • 3
  • 15
  • 32
  • Surely onPause is called then the screen goes off. What is wrong with that? – Blundell Dec 16 '11 at 17:01
  • Nothing if you need to do some work every time onPause is invoked. Unfortunately I have to do that job in onPause only if the screen is NOT turned off. So the question is: how do I realize in time that the screen is gone? BroadcastReceiver is not good: it is too slow... – lorenzoff Dec 16 '11 at 17:11
  • Could you explain the scenario, there must be a better way of doing it. Edit the question rather than a big comment. – Blundell Dec 16 '11 at 17:13
  • 1
    What job do you need to do that occurs STRICTLY if the screen is on? onPause is called when the display has been retracted from the user, implictly (figuratively and effectively) meaing the screen is "off". I'm am not seeing why it matters if the screen is off to do this job. – ahodder Dec 16 '11 at 17:18
  • 2
    Non, work related, but I love the title. It's so... dramatic. ;-) – ahodder Dec 16 '11 at 17:19
  • I did not think cared about the scenario, however: my application is a screen saver used in an industrial environment to save energy without locking the screen. It uses a AccessibilityService to keep track of user actions on the GUI to reset a timer that trigger a screen dimmer. This dimmer (an activity) re-spawn itself if another activity comes to foreground (onPause). If user push down the idle hw button, my dimmer has NOT to re-spwan itself. – lorenzoff Dec 16 '11 at 18:05
  • 1
    You could override the idle key, then in your app you can do whatever you need to do and then darken the screen if you need using this method: http://stackoverflow.com/questions/6756768/turn-off-screen-on-android – Reed Dec 16 '11 at 21:40
  • Yes Jakar, your approach has done the trick: i already have an AccessibilityService and here i trap the idle key (instead of the useless BroadcastReceiver) at global scope, otherwise i can override the onKeyDown at dimmer activity scope. I'll accept your if you propose it as an answer. – lorenzoff Dec 17 '11 at 04:56

1 Answers1

9

I had the same problem and a receiver is not the way to go

android developer

http://developer.android.com/reference/android/content/BroadcastReceiver.html

"Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack."

Instead create a power manager in your onPause method [I got it from this link]

how to find out screen status on an android device?

public void onPause(){

   PowerManager powermanager =(PowerManager)global.getSystemService(Context.POWER_SERVICE); 
   if (powermanager.isScreenOn()){
        screen_off_beforePause = false;
            //your code here
   }else{
    screen_off_beforePause = true;
   }
}

public void onResume() {
   if (screen_off_beforePause){
    Log.e(TAG + ".onResume()", "screen was off before onPause");
   }else{
    Log.d(TAG + ".onResume()", "screen was not off before onPause");
    //your code here
   }

 //resetting
 screen_off_beforePause = false;    
}
Robert
  • 6,086
  • 19
  • 59
  • 84
Rubber Duck
  • 3,673
  • 3
  • 40
  • 59