Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep my android app alive in background for 24/7 continuous sensor data collection

I have built an android app for collecting mobile and wearable sensor data. I want this app to be running in background 24/7 without being killed by OS. I am aware of all the pro and cons of running it 24/7 but that's my main business requirement.

EDIT: I have made it as foreground service and it works as long as I keep interacting with my phone but if I keep it idle for let's say 4-5 hrs OS kill it despite being listed as foreground service

like image 545
Gauranga Avatar asked Jan 10 '19 08:01

Gauranga


1 Answers

runing a background service after android marshmallow is a headache.
every background operation is suspended by Doze and StandBy Modes. and even if you use job dispatcher or WorkManger the Minimum interval to run operation is 15 mins even though you set the minimun interval less than 15 mins.
So you need to start a foreground service with sticky notification to do you work. you can take a look on this article to Start working with foreground services. don't forget to put this premission into manifest file if your app will run on androi Pie

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

and you can rerun you service on phone restart by using a broadcast receiver which listen to this action

<action android:name="android.intent.action.BOOT_COMPLETED" />

and also you can stop it by listening to this action

 <action android:name="android.intent.action.ACTION_SHUTDOWN" />
like image 123
Ramzy Hassan Avatar answered Sep 28 '22 13:09

Ramzy Hassan