在两个进程之间共享信息——什么是最安全的方式?
Share info between two processes - what's the safest way?
我有一个包含两个进程的应用程序 - MainActivity
和后台 Service
。我在两者中都需要相同的信息,并且此信息来自我的服务器(JSON 响应)。
我正在从两个进程加载此 JSON,但由于此 JSON 可能会变大,因此加载两次效率很低。
分享此信息的最安全或最佳做法是什么?
- 广播并将 JSON 从
MainActivity
复制到 Service
- 我认为这是一种安全做法,我的广播不会丢失
- 使用
SharedPreferences
将其存储在本地,并且都从这个“本地”JSON 中使用,直到应用程序关闭。根据 this question SharedPreferences
在进程之间工作正常。
非常感谢任何其他 suggestion/recommendation。
提前致谢!
最常见的方式是发送广播,而且您应该避免使用单例,有关详细信息,请参阅此 link
Android and RESTful services
不如看看Event Bus
正如文档所说:
simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues
我有一个包含两个进程的应用程序 - MainActivity
和后台 Service
。我在两者中都需要相同的信息,并且此信息来自我的服务器(JSON 响应)。
我正在从两个进程加载此 JSON,但由于此 JSON 可能会变大,因此加载两次效率很低。
分享此信息的最安全或最佳做法是什么?
- 广播并将 JSON 从
MainActivity
复制到Service
- 我认为这是一种安全做法,我的广播不会丢失 - 使用
SharedPreferences
将其存储在本地,并且都从这个“本地”JSON 中使用,直到应用程序关闭。根据 this questionSharedPreferences
在进程之间工作正常。
非常感谢任何其他 suggestion/recommendation。
提前致谢!
最常见的方式是发送广播,而且您应该避免使用单例,有关详细信息,请参阅此 link
Android and RESTful services
不如看看Event Bus
正如文档所说:
simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues