Using SharedPreferences to share data between two separate Android applications

℡╲_俬逩灬. 提交于 2020-01-17 11:14:25

问题


There will be two separate applications running on an Android device at the same time. We're responsible for App1. Here's what will be happening on the Android device:

App1 will be started first, then App2.

App1 will display a list of files containing messages.

When App2 starts, it will send a 'connect' message to App1.

App1 will allow the user to choose a file from the list.

When the user presses the 'Run' button, App1 will start sending messages every 3 seconds to App2.

App2 will send back a message after each one that it receives.

App1 will read each message that App2 sends and log it to a file.

The final message that App1 sends will result in App2 sending a disconnect back to App1.

The group responsible for App2 wants to use SharedPreferences to accomplish all this. If we do that, what do we need to use to determine when the App2 message is in the shared preference so that App1 can read it?


回答1:


even if shared preferences could be shared between process (see @CommonsWare answer..) then it sounds like the most poor design solution for the problem you describes. in fact, it smells like horrible idea, and I'm sure eventually it won't work anyway!

saying SharedPreferences is solution to communication between to different apps/ process in android, is like completely ignore all android API, and core components!

SharedPreferences not designed to be some kind of message queue between process. not even close to that!

android provides much more elegant solutions for communicating between different apps, and sharing data between them

for example:

  • remote Service binding (app1 starts service which app2 can bind to)
  • sending broadcast from one app to another when some event accures and rceive it from the other app with BroadcastReceiver
  • app1 can implement and expose ContentProvider which can be accessed from app2

and there's more!

I suggest you better understand android's core components (Service , BroadcastReceiver, Activity, ContentProvider) before you get any decision how to implement your apps. I can't imagine a way to create good functional application without using at least 3 of the above. you can varify that with reading the first page written in Android developers getting started guide - http://developer.android.com/guide/components/fundamentals.html

links:

http://developer.android.com/reference/android/app/Service.html http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/ContentProvider.html http://developer.android.com/guide/components/bound-services.html




回答2:


The group responsible for App2 wants to use SharedPreferences to accomplish all this

That is not a very good idea. Quoting the documentation for SharedPreferences:

Note: currently this class does not support use across multiple processes. This will be added later.




回答3:


As Tal said it is POOR to use Shared preference to communicate in two process, the IPC (inter process communicate, Binder in Android) with Service is a better solution to fulfill full control between two Android process. Here is a example about how to use IPC in Music player and it's client.



来源:https://stackoverflow.com/questions/17657916/using-sharedpreferences-to-share-data-between-two-separate-android-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!