远程服务和activity android之间的通信
我有一个 Activity
和几个远程服务,即使 Android 应用程序未运行,它们也应该运行。我需要服务来侦听主 Activity
的 SharedPreferences
中的更改(当其运行时),但我该如何做到这一点?
广播接收器
?工控机通讯?没有把握.. 你能帮我一下吗?
多谢, 马努。
I have an Activity
and several remote services that should be running even if the Android app is not running. I need the services to listen for changes in the SharedPreferences
of the main Activity
(when this is running), but how can I do this?
BroadcastReceiver
? IPC communication? Not sure..
Could you please give me a hand here?
Thanks a lot,
Manu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您一开始就不需要“多个远程服务”。最多,您需要几个非远程服务。远程与“即使 Android 应用程序未运行也应该运行”完全无关。这样你就浪费了大量的 RAM、CPU 和电池。
其次,由于用户和操作系统都不希望“多个”服务“即使 Android 应用程序未运行也运行”,因此请尝试将它们合并为一项服务,并且仅在该服务主动向用户交付价值时才使用该服务。用户。。例如,观察时钟滴答声并不是主动向用户传递价值——请使用
AlarmManager
来实现这一点。使它们不再是远程服务,然后让服务向
SharedPreferences
注册一个OnSharedPreferenceChangeListener
以了解首选项更改。First, you do not need "several remote services" in the first place. At most, you need several services that are not remote. Being remote has absolutely nothing to do with "should be running even if the Android app is not running". You are wasting a lot of RAM, CPU, and battery this way.
Second, since neither the user nor the OS wants "several" services "running even if the Android app is not running", please try to consolidate them into one service and only have that service when it is actively delivering value to the user. For example, watching the clock tick is NOT actively delivering value to the user -- use
AlarmManager
for that.Make them no longer be remote services, then have the service(s) register an
OnSharedPreferenceChangeListener
with theSharedPreferences
to find out about preference changes.