• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android - 决定是否在单独的进程运行服务?

用户头像
it1352
帮助1

问题说明

我正在开发一个 Android 应用程序,该应用程序会在多个小时内收集传感器数据.为此,我们有一个服务来收集传感器数据(例如加速度、GPS 等),进行一些处理并将它们远程存储在服务器上.

I am working on an Android application that collects sensor data over the course of multiple hours. For that, we have a Service that collects the Sensor Data (e.g. Acceleration, GPS, ..), does some processing and stores them remotely on a server.

目前,此服务在单独的进程中运行(在清单中使用 android:service=":background").这使活动和服务之间的通信变得复杂,但我的前任以这种方式创建了应用程序,因为他们认为将服务与活动分开会使其更稳定.

Currently, this Service runs in a separate process (using android:service=":background" in the manifest). This complicates the communication between the Activities and the Service, but my predecessors created the Application this way because they thought that separating the Service from the Activities would make it more stable.

我想要一些更实际的原因来运行一个单独的进程.有哪些优势?它真的运行更稳定吗?如果服务位于单独的进程中,服务是否不太可能被操作系统终止(以释放资源)?

I would like some more factual reasons for the effort of running a separate process. What are the advantages? Does it really run more stable? Is the Service less likely to be killed by the OS (to free up resources) if it's in a separate process?

我们的应用程序使用 startForeground() 和朋友们尽量减少被操作系统杀死的机会.

Our Application uses startForeground() and friends to minimize the chance of getting killed by the OS.

Android 文档对此不是很具体,主要说明这取决于应用程序的目的 ;-)

The Android docs are not very specific about this, the mostly state that it depends on the Application's purpose ;-)

TL;DR 将长期运行的服务放在单独的进程中(在 Android 中)的客观原因是什么?

TL;DR What are objective reasons to put a long-running Service in a separate process (in Android)?

正确答案

#1

首先是通读 组件生命周期.这样做的结果是,您确实无法保证 Service 或其他组件将被允许运行很长时间.

The first place to start is by reading through the description of component lifecycles. The take away from that is you really are not guaranteed that a Service or other component will be allowed to run for a long period of time.

但是,听起来Service 确实是您描述的功能的正确选择.这是因为您正在执行一些不是面向用户的操作.回到生命周期描述,任何时候一个 Activity 不在前台,它本质上就是一个被杀死的候选者.

However, it does sound like a Service is the right choice for the functionality you describe. This is because you are doing some operations that are not user facing. Going back to the lifecycle description, any time an Activity is not in the foreground, it is essentially a candidate for being killed.

您应该考虑使用 AlarmManager定期触发您的服务.您可能还想考虑使用@CommonsWare 创建的 WakefulIntent 库.

What you should consider doing is using AlarmManager to periodically trigger your Service. You might want also to look at using the WakefulIntent library that @CommonsWare has created.

Android 博客上有一篇很好的文章描述了多任务和进程,名为 Android 方式的多任务处理 可能会获得有关您感兴趣的进程的更多详细信息.例如:

There is a good article describing multitasking and processes on the Android blog called Multitasking the Android Way that might get at some of the more details regarding processes you are interested in. For example:

一个常见的误解Android 多任务处理的区别在进程和应用程序之间.在 Android 中,这些并不紧密耦合实体:应用程序可以似乎没有出现在用户面前当前运行的实际进程应用程序;多个应用程序可以共享流程,或一个应用程序可以使用多个进程取决于它的需要;一个或多个过程应用程序可以由Android 即使该应用程序是不主动做某事.

A common misunderstanding about Android multitasking is the difference between a process and an application. In Android these are not tightly coupled entities: applications may seem present to the user without an actual process currently running the app; multiple applications may share processes, or one application may make use of multiple processes depending on its needs; the process(es) of an application may be kept around by Android even when that application is not actively doing something.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiafb
系列文章
更多 icon
同类精品
更多 icon
继续加载