android sharedparence跨进程共享数据
2016-12-14 13:29
741 查看
http://blog.csdn.net/smile3670/article/details/21070609
String content = null;
Context c = null;
try {
c = this.createPackageContext(PREFERENCE_PACKAGE,
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "" + c);
SharedPreferences sh = c.getSharedPreference(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
content = sh.getString("tr069token3", null);
在Android 4.0上通过以上方法可以跨进程访问,之前没有加入Context.MODE_MULTI_PROCESS参数,导致不能获取到修改后的数据。在2.3的室内机上不能获取,没有权限。
要实现上面获取数据,需要在另一个共享数据的应用中,将存储数据的文件权限打开(设为只读或可读可写),才能在另一个进程中获取到数据。
下面是MODE_MULTI_PROCESS的说明:
SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all
writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.
This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
也就是说,MODE_MULTI_PROCESS这个值是一个标志,在Android 2.3及以前,这个标志位都是默认开启的,允许多个进程访问同一个SharedPrecferences对象。而以后的Android版本,必须通过明确的将MODE_MULTI_PROCESS这个值传递给mode参数,才能开启多进程访问。
String content = null;
Context c = null;
try {
c = this.createPackageContext(PREFERENCE_PACKAGE,
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "" + c);
SharedPreferences sh = c.getSharedPreference(PREFERENCE_NAME, Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
content = sh.getString("tr069token3", null);
在Android 4.0上通过以上方法可以跨进程访问,之前没有加入Context.MODE_MULTI_PROCESS参数,导致不能获取到修改后的数据。在2.3的室内机上不能获取,没有权限。
要实现上面获取数据,需要在另一个共享数据的应用中,将存储数据的文件权限打开(设为只读或可读可写),才能在另一个进程中获取到数据。
下面是MODE_MULTI_PROCESS的说明:
SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all
writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.
This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
也就是说,MODE_MULTI_PROCESS这个值是一个标志,在Android 2.3及以前,这个标志位都是默认开启的,允许多个进程访问同一个SharedPrecferences对象。而以后的Android版本,必须通过明确的将MODE_MULTI_PROCESS这个值传递给mode参数,才能开启多进程访问。
相关文章推荐
- android SharedPreferences 多进程间数据共享
- Android中SharedPreference多进程数据共享出错
- Android中SharedPreference多进程数据共享出错
- Android进程间共享数据之SharedPreference
- Android中SharedPreference多进程数据共享出错
- Android开发中多进程共享数据
- Android 跨进程数据共享
- android 多个APK共享数据 之 Shared User id
- Android通过共享用户ID实现多个Activity进程共享(SharedUserID)
- Android跨进程数据共享——ContentProvider详解
- 【Android】数据共享 sharedPreferences 相关注意事项
- android 利用SharedPreferences 在不同应用之间共享数据
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- PostgreSQL主要进程和共享缓冲区(shared buffer)----数据架构师的PostgreSQL修炼
- Android通过sharedUserId实现不同APK数据共享
- Android 多个APK共享数据(Shared User ID)
- Android ContentProvider支持跨进程数据共享与"互斥、同步"杂谈
- AndroidSharedpreference数据共享
- 【转】Android 多个APK共享数据(Shared User ID)
- Android 多个APK共享数据(Shared User ID)