gpt4 book ai didi

android - 从没有绑定(bind)的 Activity 中调用服务方法

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:19 26 4
gpt4 key购买 nike

我为我的应用程序运行了一个推送服务,负责保持 TCP 连接处于 Activity 状态并在传入消息上创建通知。

即使 Activity 已关闭,我也需要运行此服务,因此在线发布的解决方案是调用 startService()而不是 bind() ,这确实解决了服务因 Activity 被破坏而结束的问题。

但是,如果没有 Binder,我如何调用我的服务方法?我设法开始工作的一种方法是通过BroadcastReceiver .有没有更简单的方法来实现这种行为?

最佳答案

添加用于调用 startService 的操作, 一个用于您要调用/处理的每​​种不同的方法/案例,并使用 intent.getAction() 检索它, 当 onStartCommand叫做

来自 startService 的文档

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

关于android - 从没有绑定(bind)的 Activity 中调用服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30780225/

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