android - 与来自服务的 Activity (LocalService) 进行通信 - Android 最佳实践

标签 android service android-activity android-intent communication

常见场景 - 使用后台服务轮询服务器的 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?

Notify activity from service

关于android - 与来自服务的 Activity (LocalService) 进行通信 - Android 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4908267/

相关文章:

android - Activity 完成后停止android中的振动

android - 如何在一个应用程序之间向android中的另一个应用程序发送数据?

java - Activity启动时ImageView的大小

android - 在 android 服务的单独线程中运行位置更新

android - java.util.concurrent.RejectedExecutionException : pool=128/128, 队列=10/10

java - Install4j:将服务图标添加到系统托盘

asp.net-core - .net core ubuntu 使用服务时更改端口

java - 将 ActionBar 添加到 PreferenceActivity

java - Android中的Proguard和反射

android - 特定位置的 ListView setText