Android的 - 如何决定是否在一个单独的进程中运行服务? [英] Android - How to decide whether to run a Service in a separate Process?

查看:180
本文介绍了Android的 - 如何决定是否在一个单独的进程中运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个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.

目前,该服务在一个单独的进程中运行(使用的机器人:服务=:背景的清单中)。这个复杂的活动和服务之间的通信,但是我的predecessors创建的应用这种方式,因为他们的认为从活动中分离服务将使其更稳定。

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?

我们的应用程序使用<一个href="http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29">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 什么是客观原因把长时间运行在单独的进程(在安卓)服务

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

推荐答案

要启动的第一个地方是通过的组件的生命周期的。该带走的就是你真的不能保证一个服务或其它组件将被允许运行的很长一段时间。

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.

不过,这听起来像一个服务是您所描述的功能是正确的选择。这是因为你正在做一些操作不是用户面对的问题。让我们回到生命周期的说明,任何时候的活动是不是在前台,它本质上是被杀害的候选者。

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 定时触发你的服务。你可能还想看看使用 WakefulIntent 的@CommonsWare创造库。

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中这些都不是严格   加上实体:可以在应用   似乎present给用户没有   实际过程当前正在运行的   应用程序;多个应用程序可以共享   工艺,或者一个应用程序可能使   使用取决于多个进程   它的需要;的过程中(ES)   应用可以保持围绕由   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.

这篇关于Android的 - 如何决定是否在一个单独的进程中运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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