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?
That is not a very good idea. Quoting the documentation for
SharedPreferences
: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:
Service
binding (app1 starts service which app2 can bind to)BroadcastReceiver
ContentProvider
which can be accessed from app2and 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.htmllinks:
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
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.