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

你离开我真会死。 提交于 2020-08-22 06:28:59

问题


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


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/54124631/how-to-keep-my-android-app-alive-in-background-for-24-7-continuous-sensor-data-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!