简体   繁体   English

在Android应用之间共享数据的最佳做法

[英]Best practices for sharing data between android apps

I need to share data between two android apps that are on the same phone without using the internet. 我需要在不使用互联网的情况下在同一部手机上的两个Android应用之间共享数据。 The data I want to share includes a several key value pairs of different data types. 我要共享的数据包括几个不同数据类型的键值对。 The options I am aware of are: 我知道的选项有:

  1. Using intents - Send and Receive . 使用意图- 发送接收 But the typical examples for intents do not involve these use cases. 但是,意图的典型示例并不涉及这些用例。 Intents seems more suited for sharing data of a particular mime type or launch apps (or let users choose from) to handle the data being shared. 意图似乎更适合共享特定MIME类型的数据或启动应用程序(或让用户从中选择)来处理共享的数据。

  2. Sharing the SharedPreferences . 共享SharedPreferences However the MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE flags required for this have been deprecated post API 17. 但是,此要求的MODE_WORLD_READABLE或MODE_WORLD_WRITEABLE标志已在API 17之后弃用。

  3. Broadcast Receivers - But I'm not exactly listening to events. 广播接收器 -但我不是完全在听事件。 I want to use a request/response cycle. 我想使用一个请求/响应周期。

  4. Content Providers . 内容提供商 This is great except for the fact that I need to store the data in a database in the app sharing the data. 除了需要在共享数据的应用程序中将数据存储在数据库中之外,这非常好。 If I have key, value pairs where the value can be of different data types, this makes the data table a bit cumbersome. 如果我有键值对,其中值可以是不同的数据类型,那么这会使数据表有点麻烦。 Is there a better way to do this with content providers? 有没有更好的方法来与内容提供者一起执行此操作?

Which is the best technique to adopt? 最好采用哪种技术?

I created a small library that makes it simple to share SharedPreferences from one app to another in a READ ONLY fashion using ContentProvider . 我创建了一个小型库,使使用ContentProviderREAD ONLY方式将SharedPreferences从一个应用程序共享到另一个应用程序变得很容易。

Described it here - https://github.com/ratpik/android-sharedpreferences-api/blob/master/README.md 在这里描述-https: //github.com/ratpik/android-sharedpreferences-api/blob/master/README.md

You can set up Broadcast Reciever on one end to listen for particular intent type. 您可以在一端设置广播接收器以侦听特定的意图类型。 And broadcast intents from first application with data held in extras. 并从第一个应用程序中广播意图,并保留额外的数据。 You can send whole objects via intent using parcerable approach. 您可以使用可分割的方法通过意图发送整个对象。

More about parcerable approach can be found here 可以在这里找到更多关于可笑方法的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM