美文网首页
多进程通过shareUID共享数据

多进程通过shareUID共享数据

作者: GDHuo | 来源:发表于2019-04-08 19:31 被阅读0次

进程通信中可以设置使用同样的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");

可以获取到并显示

附上代码 https://github.com/GDHuo/TestShareUid

相关文章

  • 多进程通过shareUID共享数据

    进程通信中可以设置使用同样的shareUid来共享数据 这是一个例子:在app中将"123"写入到data/dat...

  • multiprocessing state

    数据共享 共享内存 server 这个服务进程是通过Manager返回的,可以包含Python对象,允许其他的进程...

  • VirtualApk源码分析-ContentProvider插件

    android通过ContentProvider可以实现进程间的数据共享,例如APP通过MediaProvider...

  • 进程间通信-利用共享内存和管道通信实现聊天窗口

    问题模型 A、B两个进程通过管道通信,A 进程每次接收到的数据通过共享内存传递给A1进程显示,同理,B进程每次接收...

  • fork与vfork的区别2020-05-04

    1.数据共享方面: fork ():子进程拷贝父进程的数据段,代码段 vfork( ):子进程与父进程共享...

  • Android跨进程通信

    Android跨进程通信:通过Intent来传递数据,共享文件和SharedPreference,基于Binder...

  • Android

    ContentProvider 作用 进程间数据共享 即跨进程通信 原理 Binder进程间通信结合匿名共享内存(...

  • IPC 使用文件共享

    共享文件也是一种不错的进程间通信方式,两个进程通信读写同一个文件夹交换数据。A进程把数据写入文件,B进程通过读取这...

  • liunx中共享内存

    实现步骤 创建共享内存区 进程1申请一块共享区域,通过shmget函数生成 映射共享内存到进程1 通过shmat实...

  • 操作系统2.20

    读者进程不会改变数据 因此可以多个读者进程同时访问共享文件 而写者进程 会改变数据 是不可以和其他进程同时访问共享...

网友评论

      本文标题:多进程通过shareUID共享数据

      本文链接:https://www.haomeiwen.com/subject/hlfuiqtx.html