Android service stops working after 1 minute! when device is not connected to power supply , but still seen in the services section of the device
-
I created an android service that issues a push-notification every 5 minutes using QTimer.
I noticed that when the service starts it has a memory consumption of around 14MB.
Sometimes, it continues to work perfectly for hours.But, usually it stops functioning after exactly 1 minute. I also noticed that it stays visible in the services section of the device however its memory consumption drops to 5.6MB after 1 minute. That's when it stops working.
Why is this happening?
Tell me if you want me to post some code.N.B: I figured out when that happens. Only when the device is not connected to power supply. ie. not charging via USB.
-
Does that only happen when you are debugging your application (i.e your phone is connected to your PC)? If so, you could check your developer options on your phone. Maybe there is a power saving option or something like that, that kicks in after one minute.
Have you looked at the output of
adb logcat
? Is there something suspicious in there? -
@Mena As far as I know, Android is allowed to kill services when it needs system resources. So before you start your application I would make sure that there aren't many applications running in the background (just to be sure that the application won't get killed due to lack of resources).
You could try to connect to your phone after your application has stopped working to run
adb logcat
. logcat usually uses a ringbuffer to store the logs, so if you are lucky (and your buffer is large enough) you can still see something. On some phones you even can configure the size of the ringbuffer in the developer settings of your phone. -
@Schluchti the service is not killed, but rather freezes during the time the screen is Off + USB not connected.
As soon as i turn on the screen it works again immediately.
I think it has something to do with this:
https://developer.android.com/training/scheduling/wakelock.htmlBut, I don't know yet, still searching....
Couldn't find smth in Logcat. -
@Mena That's possible. What do you want to do in the service? Does the service need to be running infinitely or do you just want to do a small task in the background?
There is also the possibility to run the service in the foreground. I did this for one of my applications to show the steps a user has walked in the notification bar. The disadvantage of this approach is, that it obviously drains the battery more and that the user always sees your application in the notification bar.
-
@Schluchti The service is an alarm clock. That issues a push notification when the current time is equal to the alarm time set by the user. So, I need the service to be on all the time. At least for now till I optimize the algorithm.
-
@Mena So you actually only need to run the service when the alarm fires, right? Have you considered using a
AlarmManager
, which starts the service at the given time? [1] In that case you could probably also consider starting the service as foreground service, because when the alarm goes off you usually want the user to react to the alarm (i.e turn alarm off).[1] https://stackoverflow.com/questions/8321443/how-to-start-service-using-alarm-manager-in-android
-
@Mena Sorry, I think I don't get it, so maybe this is a stupid question, but why do you need to constantly compare the current time with the alarm time in the service? Can't you just calculate the offset (seconds until the alarm will fire) and use the
AlarmManager
?e.q: User creates a new alarm at 02:00 pm and wants the alarm to go off at 08:00 pm. The difference in seconds is now 6 hrs (or 21600 seconds). So you could use the AlarmManager to trigger a alarm in 21600 secons which starts your (foreground)service. When the service starts you create your push-notification.
-
@Schluchti Well yes of course this is possible. You have a valid point!
But, maybe in the future for some reason I would need to do something else this way. So, I just want to know the solution to such problem if there is one, So I would know if I should always avoid that route.
I just want to know what the problem is? Sorry for being stubborn :DI am currently trying this:
https://stackoverflow.com/questions/6091270/how-can-i-keep-my-android-service-running-when-the-screen-is-turned-offand this:
https://stackoverflow.com/questions/5280951/android-sound-gets-disabled-after-sometime