0

I am trying to develop an android app that would continuously run in background gathering the accelerometer sensor data (every 2 minutes). I have extended the Service class and have implemented the 'onSensorChanged' method. To log the data I have used a class that writes the same in a file. The file writer class extends 'AsyncTask' and the operation has been coded in the method 'doInBackground'.

I have already tried to follow some of the links: Android App with Service Only,App run in Background, Run app in background. Even though I have tried to implement what was suggested, still the file log clearly shows that there are large portions of time (even 12 hours) with no data. Though as much as I know that accelerometer sensor always produces some data. Moreover, when I just click on the app the data logs are generated. The file is written for 15 minutes (this is also not static) and then the app doesn't log the data anymore. Any suggestion will be of immense help. Already stuck for a month. Thanks in advance.

Community
  • 1
  • 1
  • 2
    "Though as much as I know that accelerometer sensor always produces some data" -- not if the device is in sleep mode. You cannot keep the device awake indefinitely on Android 6.0+, unless your app is added to a battery optimization whitelist. – CommonsWare May 11 '17 at 15:05
  • Doze mode might be your issue: https://source.android.com/devices/tech/power/mgmt – tynn May 11 '17 at 15:05
  • Possible duplicate of http://stackoverflow.com/questions/12016623/keep-service-running – Paolo Mastrangelo May 11 '17 at 15:06
  • Thanks @CommonsWare. I have already tried to use START_STICKY. Still no luck. I have also tried using separate threads (even inside an Alarm manager) to manage the thread level parallelism. But the concern is, it seems the app is shutting down itself after some time. Can it be stopped? – Soumyajit Chatterjee May 11 '17 at 15:14
  • AFAIK there's no guarantee your service will keep running if it is not a system service (and thus require rooting). In my 5-year experience making Services, Android will eventually kill it, specially on latest versions. – m0skit0 May 11 '17 at 15:14
  • "I have already tried..." -- nothing of what you list has anything to do with the issues that I mentioned in my comment. "it seems the app is shutting down itself after some time" -- I do not know what you mean by this. – CommonsWare May 11 '17 at 15:18

3 Answers3

1

You cannot guarantee that the system won't close the service when it needs ram. Also there is doze to worry about (I don't know the exact interaction). Best you can do is start the service with START_STICKY, and the os will restart it as soon as it can.

As a solution you can look into AlarmManager, although I understand this is used more for scheduling one time operations.

Another thing to look into is the job scheduler class. If you need to support api lower than 21, there is also an open source implementation by firebase

bogdanN
  • 183
  • 10
0

If you want to have a service that run continuously you should use ForegroundService because BackgroundService can be killed by o.s. when memory is low as explained here

A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

an_droid_dev
  • 1,136
  • 14
  • 18
  • Please explain, **in detail**, how this will address the issues posed in the question. – CommonsWare May 11 '17 at 15:04
  • I can tell your from experience that even foreground services will be killed. You can Google problems users have with known foreground services like Play Music. – m0skit0 May 11 '17 at 15:17
  • @m0skit0 yes , if it is killed by the user (using some application manager) or when device is turned off. But not for memory low. – an_droid_dev May 11 '17 at 15:19
  • When device is turned off, really? I would have never guessed. Anyway, I can tell you a foreground service will be killed outside of those circutmstances. I'm still not sure why it happens, but it defiitely does happen. – m0skit0 May 11 '17 at 15:22
  • @m0skit0 of course about "turned off" i was ironic ;) . I brought this information based on the android official documentation. My own are not suppositions. – an_droid_dev May 11 '17 at 15:26
  • Dude, I'm not supposing anything. It **does** happen. I know official documentation says it doesn't, but my 5-year experience writing Services for Android says it does. And of course [I'm not the only one to have experienced this](http://stackoverflow.com/questions/6645193/foreground-service-being-killed-by-android). – m0skit0 May 11 '17 at 15:43
0

You can also create a service in a seperate thread, if you want more control over the parallelism.

AppWriter
  • 247
  • 2
  • 13