我正在为Android编写一个SDK,将被许多不同的应用程序使用。每个应用程序都需要知道是否有另一个使用该SDK的应用程序的先前安装;第一个应用程序将创建一个随机id(一个cookie)并存储它,所有后来的应用程序都必须读取它并重复使用它。请注意,每个设备都会重新创建cookie。
我已经搜索了很长时间才找到答案;在回答之前请仔细阅读,因为我已经阅读了许多不同的StackOverflow答案,并且搜索了互联网上的随机博客;我尝试了很多方法,但没有一种方法有效(我将为您保存链接)。
我已经搜索了很长时间才找到答案;在回答之前请仔细阅读,因为我已经阅读了许多不同的StackOverflow答案,并且搜索了互联网上的随机博客;我尝试了很多方法,但没有一种方法有效(我将为您保存链接)。
- 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 withgetSharedPreferences()
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
AndroidManifest.xml
中进行任何魔法或请求任何权限。可能没有完美的答案,所以也许最好的解决方案是写入外部存储器;这样做可以让事情更具上下文性,显然,在iOS上使用钥匙链存储安全数据是微不足道的。