Android的 - 这怎么可能,一个服务保留后,活动已关​​闭正在运行的? [英] Android - How is it possible, that a service keeps running after the Activity has been shut down?

查看:109
本文介绍了Android的 - 这怎么可能,一个服务保留后,活动已关​​闭正在运行的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点糊涂了服务是如何的功能,尤其是这两个东西:

http://developer.android.com/guide/topics/fundamentals /services.html

  

一个服务的启动当应用程序组件(如活动)通过调用startService启动它()。一旦开始,一个服务可以在后台运行下去,即使启动它的成分被破坏。

  

注意:服务运行在不创建自己的线程的宿主进程,该服务的主线程并不会在一个单独的进程中运行(除非另行指定)

我的问题:

  1. 这怎么可能,是由startService启动的服务()生存下去,如果主活动线程退出?或者只有那些服务生存的主要活动关机,保存在一个单独的线程?

  2. 我如何开始在一个单独的线程服务?在开发文档时的http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingService只显示了一个例子,如何产卵创建服务当一个线程......没怎么产卵服务本身在一个新的线程...
    编辑:是安卓程序= 中的manifest.xml用于在一个单独的线程开始呢? (如果它开始在一个新的过程,那么就必须在一个单独的线程..)

解决方案

要报你,

  

注意:服务运行在不创建自己的线程的宿主进程,该服务的主线程并不会在一个单独的进程中运行(除非另行指定)

在主要活动是关机,这并不一定意味着托管进程退出。根据该文件,该服务将继续运行,因为这个过程是活的!

检查

  

Android系统将试图保持过程托管服务各地只要服务已启动或已绑定到它的客户。当内存不足和需要杀死现有流程,托管服务进程的优先级将是更高

因此​​,只有当资源不足并且需要终止进程躺在身边,你的程序将被杀死。其他。这个过程住在这样会为您服务。

I am a bit confused about how services function, notably these two things:

From http://developer.android.com/guide/topics/fundamentals/services.html :

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

and:

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).

My questions:

  1. How is it possible for a service that was started by startService() survive, if the main Activity thread quits? Or only those services survive the main Activity shutdown, that are in a separate thread?

  2. How can I start a service in a separate thread? The dev doc at http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingService only shows an example how to spawn a thread when the service is created...not how to spawn the service itself in a new thread...
    EDIT: Is android:process= in manifest.xml used for starting it in a separate thread? (if it starts it in a new process, then it must be in a separate thread..)

解决方案

To quote you,:

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).

When the main activity is shutdown, it does not necessarily mean the hosting process exits. According to the documentation, the service continues to run because the process is alive!

Check this

The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher

So on only when running low on resources and it is required to kill processes lying around, your process would be killed. Else. the process lives on so would your service.

这篇关于Android的 - 这怎么可能,一个服务保留后,活动已关​​闭正在运行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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