Using SharedPreferences to share data between two

2019-08-26 06:41发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2019-08-26 07:26

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.

查看更多
ら.Afraid
3楼-- · 2019-08-26 07:36

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

查看更多
乱世女痞
4楼-- · 2019-08-26 07:38

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.

查看更多
登录 后发表回答