android动态配置shareuid,多进程通过shareUID共享数据

进程通信中可以设置使用同样的shareUid来共享数据

这是一个例子:在app中将"123"写入到data/data/xxx/file/settings.dat中,然后在secModule中取出来读取写入,并获取app中的drawable string等资源

两个应用设置android:sharedUserId="com.gdhuo.test",两个应用使用同一个加密文件加密后运行在手机中。

首先在app以及secModule中打印出自己的进程以及uid

App中

2019-04-08 10:32:08.535 13715-13715/? D/MainActivity: main process pid:13715

2019-04-08 10:32:08.535 13715-13715/? D/MainActivity: main process uid:10123

secModule中

2019-04-08 10:32:33.323 13911-13911/? D/SecActivity: second process pid:13911

2019-04-08 10:32:33.323 13911-13911/? D/SecActivity: second process uid:10123

从打出来log看属于不同的进程但是uid是一样的

2.app中通过IO流将"123"写入到settings.dat中

FileOutputStream fOut = null;

OutputStreamWriter osw = null;

try {

//默认建立在data/data/xxx/file/

fOut = openFileOutput("settings.dat", MODE_PRIVATE);

osw = new OutputStreamWriter(fOut);

osw.write(data);//data:"123"

osw.flush();

} catch (Exception e) {

...

}

secModule中通过createPackageContext("com.gdhuo.testshareuid",Context.CONTEXT_IGNORE_SECURITY);获取app的context,然后从"settings.dat"中读取及写入字符串

FileInputStream fIn = null;

InputStreamReader isr = null;

char[] inputBuffer = new char[255];

String data = null;

try {

//此处调用并没有区别,但context此时是从程序A里面获取的

fIn = context.openFileInput("settings.dat");

isr = new InputStreamReader(fIn);

isr.read(inputBuffer);

data = new String(inputBuffer);

tvTitle.setText(data);

} catch (Exception e) {

...

}

从context中获取app的drawable 以及String

Resources res = context.getResources();

int xId = res.getIdentifier("contact", "drawable", "com.gdhuo.testshareuid");

int yId = res.getIdentifier("test_str", "string", "com.gdhuo.testshareuid");

可以获取到并显示

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值