任意应用程序之间共享preferences [英] SharedPreferences between arbitrary applications

查看:167
本文介绍了任意应用程序之间共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

照片,如果你会:
应用程序A,B,C和D都需要它们之间共享一些数据量小,但也不能保证任何人将被安装。也就是说,你可能有B和D只。或A和C只等。

能否共享preferences在这种情况下使用?

我明白,我可以使用createPackageContext有A-D都使用的封装空间,如果A像一个主应用程序,总是安装,但如果我不知道这些将被安装做什么呢?如果不存在一个包空间中使用createPackageContext返回的NameNotFoundException。

我必须在这种情况下,比SD卡?

上的文件以外的任何内容提供商的选择
解决方案

  

能否共享preferences在这种情况下使用?


没有。

您不能写入共享preferences到任意位置。你能做的最好的是有一个中心的应用程序(如装载或主菜单),这是始终安装。然后,所有其他应用程序可以使用中央应用程序的preference空间来存储和共享设置(通过createPackageContext)。但是createPackageContext不会对未实际安装(例如,com.mystuff.centralstorage必须是实际安装的软件包,而不是一个任意名称)

包工作

在这种情况下,最好的解决方案是可能的SD卡,Java风格上的共享文件。请参阅:的FileInputStream,FileOutputStream中,DataInputStream和DataOutputStream类处理小,易文本文件,可以存储preferences。此外,Environment.getExternalStorageDirectory.getAbsolutePath的路径到SD卡的根目录(即,其实是必要的​​处理文件的唯一的Andr​​oid code - 剩下的就只是Java的)。

Android的ContentProviders似乎对单个应用程序更为适合。有一些允许共享(同上,与SharedPerferences),但他们似乎,每一个应用程序住在自己的私人世界里,与它们之间的共享,这是不是在这里希望拥抱Android的沙箱的概念。该解决方案需要通过任何应用数量,这在SD卡上一个普通的旧文件将是全球可访问的东西。

(多进程友好的文件保护方案是另一回事去想。)

Picture, if you will: Applications A, B, C and D. All need to share some small amount of data between them but there is no guarantee that any of them will be installed. That is, you may have B and D only. Or A and C only, etc.

Can SharedPreferences be used in this case?

I understand that I can use createPackageContext to have A-D all use the package space of A, if A is like a master-app that is always installed, but what if I don't know which of these will be installed? createPackageContext returns NameNotFoundException if used on a package space that doesn't exist.

Do I have any content provider options in this case other than a file on the SD card?

解决方案

Can SharedPreferences be used in this case?

No.

You cannot write SharedPreferences to an arbitrary location. The best you can do is have one central app (like a "loader" or "main menu") which is always installed. Then all other apps can use the central app's preference space to store and share settings (via createPackageContext). But createPackageContext will not work for a package that isn't actually installed (e.g., "com.mystuff.centralstorage" must be an actual installed package, not an arbitrary name)

In this case, the best solution is probably a shared file on the SD card, Java-style. See: FileInputStream, FileOutputStream, DataInputStream and DataOutputStream for handling small, easy text files, as might store preferences. Also, Environment.getExternalStorageDirectory.getAbsolutePath for the path to the SD card's root (that, in fact, is the only Android code necessary for handling files -- the rest can just be Java).

Android ContentProviders seem more geared towards individual apps. There is some sharing permitted (as above, with SharedPerferences) but they seem to embrace the Android "sandbox" concept where every app lives in its own private world, with sharing between them, which is not what's desired here. This solution requires something globally accessible by any number of apps, which a regular old file on the SD card would be.

(Multi-process friendly file protection schemes are another thing to think about.)

这篇关于任意应用程序之间共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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