【问题标题】:How to kill a Service by another Activity?如何通过另一个 Activity 杀死一个服务?
【发布时间】:2010-02-16 09:52:57
【问题描述】:
  1. Activity 1 使用标准 Intent 启动服务。
  2. Activity 1 启动 Activity 2。然后,Activity 1 完成()。
  3. 现在,只有活动 2。

Activity 2 如何杀死 Service,因为 Intent 是在 Activity 1 中生成的? 我不想到处传递 Intent...

【问题讨论】:

    标签: java android service android-activity android-intent


    【解决方案1】:

    一般有几种不同的方式启动服务:

    1. startService() - 之后您需要使用 stopService() 显式停止服务
    2. [bindService()][2] - 此方法允许您自动管理服务的生命周期。所以你可以让服务在最后一个客户端说 unbind() 后停止;

    详情请查看docs

    [2]:http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)

    【讨论】:

      【解决方案2】:

      你不应该关心这些事情。只需调用 stopService 并向其传递一个新的 Intent 对象。

      【讨论】:

        猜你喜欢
        • 2015-12-13
        • 2015-03-12
        • 2014-07-11
        • 2019-08-24
        • 1970-01-01
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多