How can I invoke arbitrary code in Android when the screen of the device is turned off? Is there a callback of some sort?
Asked
Active
Viewed 1,137 times
2
-
Turning the screen off doesn't invoke a callback as far as I know but you will always receive `onPause()` and `onStop()` in your activity if you're currently up front... Isn't that enough for your purposes? – avalancha Nov 19 '14 at 12:28
-
Google about Services in android – nobalG Nov 19 '14 at 12:28
-
Oh yes, I forgot about onPause() and onStop() Thank you very much ! – user5819 Nov 19 '14 at 12:29
-
Will processes that run in the background invoke some callback like the activity? – user5819 Nov 19 '14 at 12:30
-
Thank you, @mrgenco If you put it as an answer I will mark it for accepted – user5819 Nov 19 '14 at 12:36
2 Answers
0
The system will broadcast a message when the screen turns on and off. Here is the detailed answer https://stackoverflow.com/a/4208538/1581921
0
Build a BroadcastReceiver on the on/off screen. The Android system broadcasts a message when certain things happen ( like the screen going on/off , battery changes etc. ) With the BroadCastReceiver you will tell your app how to react when the screen will go off.

BMU
- 429
- 2
- 9