gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:40:58 28 4
gpt4 key购买 nike

当我注意到两个矛盾点时,我正在浏览 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com