基于MemoryFile & AIDL
该方案只适配TargetSDK<=27的场景(原因是用到反射机制)
Demo:http://xhrong.github.io/attachments/ShareMemory.rar
参考:https://www.cnblogs.com/jiaoxiake/p/6970123.html
基于三方库(NewtronLabs/SharedMemory)
Shared Memory
The Shared Memory library allows for the creation of memory regions that may be simultaneously accessed by multiple Android processes or applications. Developed to overcome the Android 1MB IPC limitation, this Shared Memory library allows you to exchange larger amounts of data between your Android applications.
How to Use
Setup
Include the below dependencies in your build.gradle
project.
1 | buildscript { |
In the build.gradle
for your app.
1 | dependencies { |
Sharing Memory - Producer
From the application that wishes to shared its memory, allocate a shated memory region with a given name.
1 | // Allocate 2MB |
Write data to memory:1
2byte[] strBytes = "Hello World!".getBytes();
sharedMemory.writeBytes(strBytes, 0, 0, strBytes.length);
Once an application has shared a memory region it can be accessed by other processes or application which are aware of it.
Accessing Shared Memory - Consumer
In order for an application to access a region of memory shaered by an external application perform the following:
1 | // This is the application id of the application or process which shared the region. |
Additional Samples
A set of more complex exmaples can be found in this repo’s samples folders: SmProducer and SmConsumer.