简体   繁体   English

推迟销毁Android Bound Service

[英]Postpone destruction of Android Bound Service

I have an Android Bound Service that handles multiple client connections. 我有一个处理多个客户端连接的Android绑定服务。 If I understand correctly, Android kills the service if there is no client connections. 如果我理解正确,如果没有客户端连接,Android将终止该服务。

At this point, however, my service may still have another thread running up to 1 minute after clients disconnect. 但是,在这一点上,客户端断开连接后最多1分钟,我的服务仍可能有另一个线程运行。 My concern is that, if I join this thread before my service is destroyed, the destruction is delayed and it will prevent creating a new service, dropping new client connections. 我担心的是,如果我在服务被销毁之前加入了该线程,销毁将被延迟,这将阻止创建新服务并丢弃新的客户端连接。 Is this true? 这是真的? Is there a way to postpone the destruction of bound services? 有没有办法推迟绑定服务的销毁? (Like adding a fake bound count to the service when the worker thread starts and subtracting when the thread ends.) (就像在工作线程启动时向服务中添加伪绑定计数,在线程结束时相减。)

Basically, the case I want to cover is: 基本上,我想讲的案例是:

  1. The service have a long-running task in a separate thread. 该服务在单独的线程中具有长期运行的任务。
  2. The number of client connection reaches zero. 客户端连接数达到零。
  3. The service is not killed yet because the child thread is still running. 该服务尚未终止,因为子线程仍在运行。

    3-1. 3-1。 The child thread is done, and then the service is killed. 子线程完成,然后服务被终止。

    or 要么

    3-2. 3-2。 While the child thread is running, a new client binds. 子线程运行时,将绑定一个新客户端。 So the service keeps running. 因此该服务保持运行。

This is exactly how a Service works that is started with startService() . 这就是以startService()开头的Service工作方式。 Commonly one starts the service with that call before each binding to the service. 通常,在每次绑定到该服务之前,都会通过该调用启动该服务。

stopSelf() or Context.stopService() will stop the service immediately or as soon as all bounders are undbounded. stopSelf()Context.stopService()将立即或在所有边界解除绑定后立即停止服务。

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

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