如何在24/7连续传感器数据收集中保持我的Android应用程序在后台运行 [英] How to keep my android app alive in background for 24/7 continuous sensor data collection

查看:146
本文介绍了如何在24/7连续传感器数据收集中保持我的Android应用程序在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个Android应用,用于收集移动和可穿戴传感器数据.我希望这个应用程序可以在24/7后台运行,而不会被操作系统杀死.我知道24/7全天候运行的利弊,但这是我的主要业务需求.

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.

我将其作为前台服务,并且只要我与手机保持互动,它就可以正常工作,但是如果我将其闲置一段时间,则可以说尽管被列为前台服务,但操作系统仍在4-5小时内将其杀死

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

推荐答案

在运行Android棉花糖后运行后台服务令人头疼.
每个后台操作都由休眠和待机"模式暂停. 即使您使用作业调度程序或WorkManger,即使将最小间隔设置为小于15分钟,运行操作的最小间隔也是15分钟.
因此,您需要启动带有粘性通知的前台服务才能正常工作.您可以查看文章以开始使用前台服务. 如果您的应用程序将在androi Pie上运行

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

这篇关于如何在24/7连续传感器数据收集中保持我的Android应用程序在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆