使用共享preferences分享两个独立的Andr​​oid应用程序之间的数据 [英] Using SharedPreferences to share data between two separate Android applications

查看:207
本文介绍了使用共享preferences分享两个独立的Andr​​oid应用程序之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有将在同一时间在Android设备上运行两个单独的应用程序。我们负责对APP。以下是将在Android设备上发生的事情:

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的将首先启动,然后App2的。

App1 will be started first, then App2.

App1的将显示包含消息的文件的列表。

App1 will display a list of files containing messages.

当App2的启动时,它就会发出一个'连接'消息App1的。

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

App1的将允许用户从列表中选择一个文件。

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

当用户presses的运行按钮,程序App1将开始发送信息每3秒App2的。

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

App2的将其接收到的每一个后发回的消息。

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

App1的将读取App2的发送消息并将其记录到文件中。

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

这App1的发送将导致App2的发送断开回App1的,最终的消息

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

负责App2的群体希望使用共享preferences来完成这一切。如果我们这样做,有什么事我们需要使用以确定何时App2的消息是在共享preference使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?

推荐答案

即使共享preferences能过程之间共享(见@CommonsWare答案..)
那么它听起来像你描述问题的最可怜的设计解决方案。
事实上,它闻起来像可怕的想法,我敢肯定,最终还是无论如何都不会工作!

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!

共享preferences 是解决通信不同的应用程序/ android的过程中,之间就像是完全忽略所有Android API和核心部件!

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

共享preferences 没有设计成某种进程间消息队列。
甚至没有接近!

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

Android提供更优雅的解决方案,为不同的应用程序之间的通信,并把它们之间共享数据。

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

例如:


  • 远程服务绑定(APP1启动服务,APP2可以绑定到)

  • 从一个应用程序发送广播到另一个某些事件accures并与来自其他应用程序rceive它广播接收器

  • APP1可以实现和揭露的ContentProvider 可从APP2访问

  • 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

和还有更多!

我建议你更好地了解Android的核心组件(服务广播接收器活动的ContentProvider ),你得到的任何决定如何实现您的应用程序之前。
我无法想象的方式,而不使用上述至少3创造良好的功能应用。
你可以varify与读出写在Android开发的第一页入门指南 - 的http:// developer.android.com/guide/components/fundamentals.html

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

链接:

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

这篇关于使用共享preferences分享两个独立的Andr​​oid应用程序之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆