【问题标题】:Do I need to call both unbindService and stopService for Android services?我需要为 Android 服务调用 unbindService 和 stopService 吗?
【发布时间】:2011-03-24 01:38:53
【问题描述】:

在我的 Android 应用中,我同时调用 startServicebindService

Intent intent = new Intent(this, MyService.class);
ServiceConnection conn = new ServiceConnection() { ... }

startService(intent)
bindService(intent, conn, BIND_AUTO_CREATE);

后来,我尝试了unbindService andstopService`:

unbindService(conn);
stopService(intent);

但是,我在调用 unbindService 时遇到异常。如果我删除此调用,该应用似乎可以通过stopService 调用正常运行。

我做错了吗?我认为bindService 调用必须与unbindService 调用相关联,startService 调用必须与stopService 调用相关联。不过,这里的情况似乎并非如此。

【问题讨论】:

  • 你有没有想过这个问题?我遇到了同样的事情,并为自己注释掉了 unbindService(conn)。似乎工作正常,但就像你一样,我的直觉在后台说不喜欢它。

标签: android serviceconnection


【解决方案1】:

如您所见here,这取决于您要实现的目标以及绑定服务的方式。如果要长时间引用服务,最好使用bindService而不是startService。如果在 bindService 方法中使用了标志 BIND_AUTO_CREATE,那么您不必调用 startService,因为服务会在必要时自行启动。

如果您调用 unBind 服务,那么您与该服务的关联将被删除。您不必明确停止服务,但可以。但需要注意的是,如果调用 unBind(),则服务可以随时停止。

【讨论】:

  • 谢谢,但我去过那里,如果我在使用 BIND_AUTO_CREATE 标志绑定到它之前手动启动它,是否需要取消绑定和停止服务。
  • But it's importatnt to note, that if you call unBind(), then the service is allowed to stop at any time. — 不,如果它已启动,它不会停止,直到调用 stopSelfstopService: the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients. developer.android.com/guide/components/bound-services#Lifecycle
【解决方案2】:

stopService() 的 Android 文档指出:

请注意,如果停止的服务仍然有绑定了 BIND_AUTO_CREATE 设置的 ServiceConnection 对象,则在删除所有这些绑定之前,它不会被销毁。有关服务生命周期的更多详细信息,请参阅服务文档。

所以首先调用stopService() 然后调用unbindService() 应该可以工作(它对我有用)。

【讨论】:

  • 啊,听起来我对 unbindServicestopService 的排序是倒退的,我明白了。
  • 谢谢,这对我有帮助!我使用了 BIND_AUTO_CREATE 并且 unbindService() 帮助解决了 ServiceConnection 泄漏的问题。
  • @Matt Huggins 所以你们的意思是先解除绑定然后停止是吗?
  • 先调用 stopService 然后 unbindService 为我工作。
  • the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients. developer.android.com/guide/components/bound-services#Lifecycle
【解决方案3】:

我遇到的一个问题:

确保在调用 bindService 的同一上下文中调用 unbindService。就我而言,我正在执行以下操作来绑定它:

Context c = getApplicationContext();
c.bindService(...);

然后解除绑定,只需:

unbindService(...);

确保 bind 和 unbind 使用相同的上下文可以解决问题。

【讨论】:

    【解决方案4】:

    要直接回答您的问题,不调用 unbindservice 是“可以的”,但不建议这样做。发生的情况是,如果您的服务不是前台服务,android 系统可能会杀死它以释放内存。如果还有一些组件绑定到它,但你没有调用 unbind,它就会被 android 系统解绑。

    在此处查看 android 文档

    https://developer.android.com/guide/components/bound-services

    如果当您的应用销毁 客户端,销毁会导致客户端解除绑定。这是更好的做法 与客户端交互完成后立即解除绑定 服务。这样做可以关闭空闲服务。更多 有关绑定和解除绑定的适当时间的信息,请参阅附加 注释。

    【讨论】:

    • 引用的文档似乎是关于在可能的情况下不会停止服务的情况(因为不需要它),但如果你停止服务,它似乎不再那么清楚了如果你应该也可以解绑。
    • the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients. developer.android.com/guide/components/bound-services#Lifecycle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多