简体   繁体   中英

Share a piece of data between several Android apps

I am writing an SDK for Android that will be used by many different apps. Each app needs to know if there is a previous installation of another app that uses the SDK; the first one will create a random id (a cookie) and store it, all later apps have to read it and reuse it. Note that the cookie will be created anew for every device.

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; I have tried a lot of things but none worked (I will save you the links).

  • A ContentProvider is definitely overkill. Also it needs to intrude an app's AndroidManifest.xml , something I am trying to avoid.
  • 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.
  • 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.
  • I can get a common preferences object using 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:

     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.

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.

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. 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.

I haven't actually tried this, but I imagine it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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