简体   繁体   English

在多个Android应用之间共享数据

[英]Share a piece of data between several Android apps

I am writing an SDK for Android that will be used by many different apps. 我正在编写一个Android版SDK,将被许多不同的应用程序使用。 Each app needs to know if there is a previous installation of another app that uses the SDK; 每个应用程序都需要知道以前是否安装了另一个使用SDK的应用程序; the first one will create a random id (a cookie) and store it, all later apps have to read it and reuse it. 第一个将创建一个随机ID(一个cookie)并存储它,所有后来的应用程序必须读取它并重用它。 Note that the cookie will be created anew for every device. 请注意,将为每个设备重新创建cookie。

I have searched for a long time for the answer; 我找了很长时间才得到答案; please read thoroughly before answering because I have read lots of different StackOverflow answers and have scoured the internet reading random blogs; 请在回答之前仔细阅读,因为我已经阅读了很多不同的StackOverflow答案并且已经在互联网上阅读随机博客; I have tried a lot of things but none worked (I will save you the links). 我尝试过很多东西,但都没有用(我会保存链接)。

  • A ContentProvider is definitely overkill. ContentProvider绝对是矫枉过正的。 Also it needs to intrude an app's AndroidManifest.xml , something I am trying to avoid. 此外,它需要侵入应用程序的AndroidManifest.xml ,这是我试图避免的。
  • Likewise for a service: needs to be defined in the AndroidManifest.xml , which I do not control and do not want to require changes to. 同样对于服务:需要在AndroidManifest.xml定义,我无法控制并且不想要求更改。
  • Using external storage would perhaps be an option, but I don't want to require additional permissions. 使用外部存储可能是一种选择,但我不想要额外的权限。
  • Using SharedPreferences directly with getSharedPreferences() does not work because apps can only access their own preferences. 直接在getSharedPreferences()中使用SharedPreferences不起作用,因为应用只能访问自己的首选项。
  • I can get a common preferences object using createPackageContext(package, MODE).getSharedPreferences() . 我可以使用createPackageContext(package, MODE).getSharedPreferences()获得一个公共首选项对象。 This would work beautifully if I had a main package and many clients of the data, but in fact I don't know the package of the app that will be installed first -- it can be any of them. 如果我有一个主程序包和许多数据客户端,这将很好地工作,但实际上我不知道将首先安装的应用程序包 - 它可以是任何一个。 I don't even have a list of package names to search. 我甚至没有要搜索的包名列表。 Passing a package name which has not been installed fails. 传递尚未安装的包名称失败。
  • Following the approach above, I have tried to piggyback on some standard Android app which I can count on, and store my preferences there -- say: 按照上面的方法,我试图捎带一些我可以信赖的标准Android应用程序,并将我的偏好存储在那里 - 比如说:

     createPackageContext( "com.android.browser", Context.CONTEXT_RESTRICTED) .getSharedPreferences( "GLOBAL_PREFS", Context.MODE_WORLD_READABLE); 

    but it does not work: the error reads 但它不起作用:错误读取

     Couldn't create directory for SharedPreferences file /data/data/com.android.browser/shared_prefs/GLOBAL_PREFS.xml 

So, to recap: I need to store a piece of data (a short string) in some standard location, so that any other app can go there and read it if present, and it should work if at all possible without doing any magic in the AndroidManifest.xml or requesting any permissions. 所以,回顾一下:我需要在一些标准位置存储一段数据(一个短字符串),这样任何其他应用程序都可以去那里阅读它(如果存在的话),它应该可以工作,如果可能的话没有做任何魔术AndroidManifest.xml或请求任何权限。

There probably isn't any perfect answer, so perhaps the best solution is to write to external storage; 可能没有任何完美的答案,所以也许最好的解决方案是写入外部存储; then so be it. 那就这样吧。 To put things into context, apparently it is trivial to do this on iOS using a keychain, designed to store secure data. 为了把事情放到上下文中,显然在iOS上使用设计用于存储安全数据的钥匙串来做这件事是微不足道的。

Unfortunately there really isn't a great answer for this that I know of. 不幸的是,我所知道的确没有很好的答案。 You've come up with a pretty good outline of your options and the best way may well be with external storage. 您已经提出了很好的选项轮廓,最好的方法可能是外部存储。

Just to throw something out there, I suppose it's possible you could use a flat file with a fixed name and world readable (and possibly writable) permissions. 只是为了抛出一些东西,我想你可以使用一个具有固定名称和世界可读(可能是可写)权限的平面文件。 You'd have to then iterate through all applications' directories and check for this known-named file in each folder and attempt to open it. 然后,您必须遍历所有应用程序的目录,并在每个文件夹中检查此已知命名的文件并尝试打开它。

While this might work theoretically, consider the case where the app that contains the "cookie" is uninstalled. 虽然这在理论上可能有效,但请考虑卸载包含“cookie”的应用程序的情况。 Then you're left cookie-less. 那你就没有了。 You might want to create the cookie in every app, copying over the value of the previous cookies to new cookies. 您可能希望在每个应用程序中创建cookie,将以前cookie的值复制到新cookie。

I haven't actually tried this, but I imagine it should work. 我实际上没有试过这个,但我想它应该有效。

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

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