如何将回调放入 Intent 中?
我想启动一个带有包含回调的意图的服务。我怎样才能做到这一点?我想使用startService(...),而不是绑定机制。
I want to a start a Service with an Intent that contains a callback. How can I do that? I want to use startService(...), not the binding mechanism.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用由服务发送的自定义广播。您可以在您的活动中注册该广播并收听该广播。这将是服务回调的标准解决方案。您不能将复杂的数据(如引用)放入您的意图中,只能将简单和原始的数据放入...
自定义意图和接收器广播
根据评论更新
如何解释此演示文稿 然后(幻灯片 11,左下角的数字角落)?
在本例中,他们使用了服务助手。我认为他们用
startService
启动服务器来象征服务应该保持唤醒状态。当绑定活动被销毁时,bindService
将停止服务。无论如何,服务助手都会调用bindService
,因为它显示10。活页夹回调
。因此,最终帮助程序似乎是请求活动的绑定包装器,因此活动不必处理整个服务通信。这就是我的解释方式...
I would recommend to use a custom broadcast which will be sent by the service. You can register for that broadcast in your activity and catch that. That would be the standard solution for a service callback. You can't put complex data (like references) in your intent, simple and primitive data only...
Custom Intents and Broadcasting with Receivers
Update based on comment
How to interpret this presentation then (slide 11, number in bottom left corner)?
In this case they're using a service helper. I think they start the server with
startService
to symbolize that the service should stay awake. AbindService
would stop the service when the binding activity is destroyed. The service helper will callbindService
anyway because it says10. Binder callback
. So in the end the helper seems to be a bind wrapper for the requesting activity so the activities have not to deal with the whole service communication.That's the way I would interpret it...