简体   繁体   English

Android应用程序可以在后台连续运行吗?

[英]Can an android app run in background continuously?

I am trying to develop an android app that would continuously run in background gathering the accelerometer sensor data (every 2 minutes). 我正在尝试开发一个Android应用程序,该应用程序将在后台连续运行,以收集加速度计传感器数据(每2分钟一次)。 I have extended the Service class and have implemented the ' onSensorChanged ' method. 我扩展了Service类并实现了“ onSensorChanged ”方法。 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 '. 文件编写器类扩展了“ AsyncTask ”,并且该操作已在方法“ doInBackground ”中进行了编码。

I have already tried to follow some of the links: Android App with Service Only , App run in Background , Run app in background . 我已经尝试遵循一些链接: 仅服务的Android应用程序, 在后台 运行的应用程序,在后台运行的应用程序 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. 即使我尝试实现建议的内容,文件日志仍然清楚地显示出有很大一部分时间(甚至12小时)没有数据。 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. 该文件将写入15分钟(这也不是静态的),然后该应用程序不再记录数据。 Any suggestion will be of immense help. 任何建议都会有很大帮助。 Already stuck for a month. 已经卡住了一个月。 Thanks in advance. 提前致谢。

You cannot guarantee that the system won't close the service when it needs ram. 您不能保证系统在需要ram时不会关闭服务。 Also there is doze to worry about (I don't know the exact interaction). 另外还有打do睡的问题(我不知道确切的交互作用)。 Best you can do is start the service with START_STICKY, and the os will restart it as soon as it can. 最好的办法是使用START_STICKY启动服务,操作系统会尽快将其重新启动。

As a solution you can look into AlarmManager, although I understand this is used more for scheduling one time operations. 作为解决方案,您可以研究AlarmManager,尽管我知道这更多用于计划一次操作。

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 如果您需要支持低于21的api, firebase还提供了一个开源实现

If you want to have a service that run continuously you should use ForegroundService because BackgroundService can be killed by os when memory is low as explained here 如果你想有一个连续运行,你应该使用一个服务ForegroundService因为BackgroundService可以通过OS被杀死时内存不足的解释在这里

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. 这意味着除非服务停止或从前台删除,否则无法取消该通知。

如果要对并行性进行更多控制,还可以在单​​独的线程中创建服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM