-1

How do i prevent my app from closing when the device goes to sleep mode, i obsevered that anytime my device goes to sleep mode my app closes but i want it still open when my devices comes out of sleep mode.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • possible duplicate of [prevent mobile from going into sleep mode when app is running](http://stackoverflow.com/questions/5155720/prevent-mobile-from-going-into-sleep-mode-when-app-is-running) – AAnkit Mar 02 '15 at 14:13
  • @AndroidGeek I have seen it, and it didnt help, so i had to ask. I will appreciate if you can help, thank you –  Mar 02 '15 at 14:15
  • @ AndroidGeek - the problem is, and I faced the same, You can prevent the device go to sleep, but You cannot prevent the user to set the screen to sleep.... – Opiatefuchs Mar 02 '15 at 14:20
  • what I have done was, register a broadcastreceiver and a service, which get fired when the screen goes on or off. When it goes off, I stored a boolean value in sharedPreferences, that this activity was alive at go to sleep mode. If screen goes on again, the service checks the value and if the activity is still alive, service is doing nothing. but if activity is not alive anymore, the service starts it again... – Opiatefuchs Mar 02 '15 at 14:23
  • Had you tried using wakelock. – Surender Kumar Mar 02 '15 at 14:28
  • Please post your code here on SO, in order your question could be useful to someone else. – grattmandu03 Mar 02 '15 at 14:28
  • @Opiatefuchs can you please kindly help make an edit to it, using my present code, i will gladly accept your answer –  Mar 02 '15 at 14:28
  • @SurenderKumar yes but it drains my battery i noticed and doesnt work so effectively –  Mar 02 '15 at 14:30
  • You have to remove finish() from onpause that causes your application to close. – Surender Kumar Mar 02 '15 at 14:32

1 Answers1

2

Probably it is because you call the finish() method in the onPause().

Morry
  • 726
  • 4
  • 11
  • so what should i do, please help!! –  Mar 02 '15 at 14:27
  • Remove finish() method from onPause(). – Morry Mar 02 '15 at 14:28
  • this is correct, but soon or later, if the screen goes off, the app will be destroyed. For example, the user sends the screen to sleep via power button and will not return for the next few hours... – Opiatefuchs Mar 02 '15 at 14:32
  • @Morry it works but this now prevents the app, from going to sleep mode but i would like that app to go to sleep mode –  Mar 02 '15 at 14:52
  • I don't understand... why the app shouldn't go to sleep mode? – Morry Mar 02 '15 at 15:16