跨进程共享 SharedPreferences

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.getSharedPreferences(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参数,才能开启多进程访问。



http://blog.chinaunix.net/uid-20771867-id-3085191.html

http://blog.csdn.net/elfylin/article/details/5988513 各个模式下生成的xml权限不一样?

http://www.eoeandroid.com/thread-181297-1-1.html Context.MODE_PRIVATE存储的数据是不能共享的?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值