android - 启动和绑定(bind)的服务何时销毁?

标签 android android-service

当我注意到两个矛盾点时,我正在浏览 android 中的服务文档:

在服务文档中,它在 Managing the Lifecycle of a Service 中指定。

These two paths are not entirely separate. That is, you can bind to a service that was already started with startService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

但是在Managing the Lifecycle of a Bound Service 中关于绑定(bind)服务的文档中

However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.

可能是我,但我认为这些陈述是矛盾的。谁能澄清一下...

最佳答案

同意文档可以更清晰。他们想说的是:

  • 如果您调用 startService(),则服务将继续运行,除非您调用 stopSerivce()(或从服务内调用 stopSelf())
  • 如果您调用 bindService(),则服务将继续运行,除非您调用 unbindService()
  • 因此,如果您同时调用 startService() 和 bindService(),则服务将继续运行,直到您同时调用 stopService 和 unbindService()。两者都不会自行停止服务。

创建了一个非常简单的 Activity 和 Service,并运行了以下启动/停止/绑定(bind)/取消绑定(bind)的序列。我观察到这些调用给出了以下结果。

绑定(bind)-解除绑定(bind)

bindService() caused:
    onCreate()
    onBind()
unbindService() caused:
    onUnbind()
    onDestroy()

开始-绑定(bind)-取消绑定(bind)-停止

startService() caused:
    onCreate()
    onStartCommand()
bindService() caused:
    onBind()
unbindService() caused:
    onUnbind()
stopService() caused:
    onDestroy()

开始-绑定(bind)-停止-解除绑定(bind)

startService() caused:
    onCreate()
    onStartCommand()
bindService() caused:
    onBind()
stopService() caused:
    -- nothing
unbindService() caused:
    onUnbind()
    onDestroy()

绑定(bind)-开始-停止-解除绑定(bind)

bindService() caused:
    onCreate()
    onBind()
startService() caused:
    onStartCommand()
stopService() caused:
    -- nothing -- still running
unbindService() caused:
    onUnbind()
    onDestroy()

绑定(bind)开始-解除绑定(bind)-停止

bindService() caused:
    onCreate()
    onBind()
startService() caused:
    onStartCommand()
unbindService() caused:
    onUnbind()
stopService() caused:
    onDestroy()

如您所见,在调用 bind 和 start 的每种情况下,服务都会一直运行,直到 unbind 和 stop 都被调用。解除绑定(bind)/停止的顺序并不重要。

这是从我的简单测试应用中的单独按钮调用的示例代码:

public void onBindBtnClick(View view) {
    Intent intent = new Intent(MainActivity.this, ExampleService.class);
    bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

public void onUnbindBtnClick(View view) {
    if (serviceIsBound) {
        unbindService(serviceConnection);
        serviceIsBound = false;
    }
}

public void onStartBtnClick(View view) {
    Intent intent = new Intent(MainActivity.this, ExampleService.class);
    startService(intent);
}

public void onStopBtnClick(View view) {
    Intent intent = new Intent(MainActivity.this, ExampleService.class);
    exampleService.stopService(intent);
}

关于android - 启动和绑定(bind)的服务何时销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17146822/

相关文章:

android - 有没有更好的方法在 UGUI(Unity 新 UI)中使用透明图像来阻止光线转换?

Android - JNI 指南

android - 来自其他应用程序但相同用户标识/进程的 bindService

android - 我应该在android服务的onDestroy方法中调用onCreate方法吗?

android - 如何设置 Android 闹钟在特定日期之前触发

android - 程序类型已经存在 : okhttp3. Authenticator$1

android - 我在哪里创建和使用 ScheduledThreadPoolExecutor、TimerTask 或 Handler?

javascript - Jquery/JavaScript OnClick 事件未在移动设备上触发

android - IntentService STICKY 已销毁但服务在 Android 2.3.4 中保持记录数据

当应用程序终止时 Android 服务也会终止