Reputation: 63
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.
Upvotes: 0
Views: 1214
Reputation: 1136
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.
Upvotes: 0
Reputation: 193
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
Upvotes: 1
Reputation: 245
You can also create a service in a seperate thread, if you want more control over the parallelism.
Upvotes: 0