远程服务和activity android之间的通信

发布于 2024-11-29 10:09:34 字数 223 浏览 1 评论 0原文

我有一个 Activity 和几个远程服务,即使 Android 应用程序未运行,它们也应该运行。我需要服务来侦听主 ActivitySharedPreferences 中的更改(当其运行时),但我该如何做到这一点?

广播接收器?工控机通讯?没有把握.. 你能帮我一下吗?

多谢, 马努。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-12-06 10:09:35

我有一个 Activity 和几个远程服务,即使 Android 应用程序未运行,它们也应该运行。

首先,您一开始就不需要“多个远程服务”。最多,您需要几个非远程服务。远程与“即使 Android 应用程序未运行也应该运行”完全无关。这样你就浪费了大量的 RAM、CPU 和电池。

其次,由于用户和操作系统都不希望“多个”服务“即使 Android 应用程序未运行也运行”,因此请尝试将它们合并为一项服务,并且仅在该服务主动向用户交付价值时才使用该服务。用户。。例如,观察时钟滴答声并不是主动向用户传递价值——请使用 AlarmManager 来实现这一点。

我需要服务来侦听主 Activity 的 SharedPreferences 中的更改(当其运行时),但我该如何做到这一点?

使它们不再是远程服务,然后让服务向 SharedPreferences 注册一个 OnSharedPreferenceChangeListener 以了解首选项更改。

I have an Activity and several remote services that should be running even if the Android app is not running.

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.

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?

Make them no longer be remote services, then have the service(s) register an OnSharedPreferenceChangeListener with the SharedPreferences to find out about preference changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文