常见场景 - 使用后台服务轮询服务器的 Activity 。
Service 将通过 AlarmManager 定期运行,并为 Activity 执行任务(用户点击按钮,从服务器获取内容)。
我想了解这里的最佳做法。我认为最好的设计是 Android LocalService 示例: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
但在示例中,Activity 引用了 Activity mBoundService
但没有反向连接(Service 无法调用 Activity)。 p>
Service 调用 Activity 的最佳方式是什么?
我是否使用 Intent 、广播接收器、消息?如何?
最佳答案
I think the best design would be the Android LocalService example: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
我不会。使用您能承受的尽可能松的联轴器。因此,平均而言,目标是使用 startService()
的命令模式,而不是使用 bindService()
的绑定(bind)模式。值得注意的是,在处理配置更改(例如,屏幕旋转)时,绑定(bind)有点麻烦。
What is the best way for the Service to call the Activity? Do I use Intents, BroadcastReceivers, Messages? How?
关于android - 与来自服务的 Activity (LocalService) 进行通信 - Android 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4908267/