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

Viewed 11937

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

3 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" />

Use Service for collecting data you can run it foreground and it will not be killed by OS

Make the application into a launcher and set it as a default launcher. You can follow this guide here. The idea is the launcher is launched by the OS when the phone boot and it is never killed when system runs out of memory.

Related