Android 匿名共享内存demo篇(一)

    Android系统的IPC方式通常为:文件、socket、binder、messenger、AIDL、ContentProvider,此外还有个Anonymous Shared Memory(匿名共享内存),这篇文章介绍Ashm基础使用相关知识。

Android api27加入了SharedMemory类,27之前需要通过反射去拿到文件描述符。

    demo为MainActivity和一个远程服务(指定了process的service)之间的通信,步骤如下:

1、创建aidl文件IMemoryAidlInterface

interface IMemoryAidlInterface {
   ParcelFileDescriptor getParcelFileDescriptor();
}

1、创建一个服务并在manifest文件中指定process

public class MemoryFetchService extends Service {
    private static final String TAG = "MemoryFetchService";
    private static final String SHM_FILE_NAME = "test_memory";
    @Override
    public IBinder onBind(Intent intent) {
        return new MemoryFetchStub();
    }
    static class MemoryFetchStub extends IMemoryAidlInterface.Stub {
        @Override
        public ParcelFileDescriptor getParcelFileDescriptor() throws RemoteException {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O_MR1) {
                MemoryFile memoryFile = null;
                try {
                    memoryFile = new MemoryFile(SHM_FILE_NAME, 1024);
                    memoryFile.getOutputStream().write(new byte[]{1, 2, 3, 4, 5});
                    Method method = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
                    FileDescriptor des = (FileDescriptor) method.invoke(memoryFile);
                    return ParcelFileDescriptor.dup(des);
                } catch (Exception e) {
                    Log.d(TAG, "getParcelFileDescriptor: exception : " + e.toString());
                }
            }else {
                //TODO use SharedMemory to get fd

            }

            return null;
        }
    }
}
<service android:name=".MemoryFetchService"
            android:process=":ashm"/>

在服务MemoryFetchService中创建共享内存虚拟文件并设置size,写入内容为一个数组。

在MainActivity中绑定服务,读取共享内存文件中的内容:

private void bind() {
        Intent intent = new Intent(MainActivity.this, MemoryFetchService.class);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                byte[] content = new byte[10];
                IMemoryAidlInterface iMemoryAidlInterface
                        = IMemoryAidlInterface.Stub.asInterface(service);
                try {
                    ParcelFileDescriptor parcelFileDescriptor = iMemoryAidlInterface.getParcelFileDescriptor();
                    FileDescriptor descriptor = parcelFileDescriptor.getFileDescriptor();
                    FileInputStream fileInputStream = new FileInputStream(descriptor);
                    int read = fileInputStream.read(content);
                    Log.d(TAG, "onServiceConnected: read == " + read);
                } catch (Exception e) {
                    Log.d(TAG, "onServiceConnected: exception: " + e.toString());
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, Service.BIND_AUTO_CREATE);
    }

debug可以看到content中的内容即为在MemoryFetchService中写入的内容。

adb shell进入设备后,通过lsof|grep "test_memory"可以查看到创建的匿名共享内存虚拟文件:

130|Galileo:/ # lsof |grep "test_memory"                                                                                                                                                                    
com.***.ash 27421     u0_a73  mem   unknown                                     /dev/ashmem/test_memory (deleted)

到这里Android匿名共享内存简单使用demo已经完了,代码在这里clone。

Android系统源代码情景分析的评论 这本书是我看过的最深入的android书了,可以看出作者是个很有悟性的程序员,很适合需要提高的android框架层工程师进阶。binder部分是目前所有书中分析的最全面的。匿名共享内存分析的也很好。 情况分析应该是学毛德操老师的,作者确实做到了,作者在讲解时,会从java层到native层,再到linux kernel中整个串起来讲。使读者可以完全了解某些子系统的运行机制 内容简介 · · · · · · 在内容上,本书结合使用情景,全面、深入、细致地分析Android系统的源代码,涉及到Linux内核层、硬件抽象层(HAL)、运行时库层(Runtime)、应用程序框架层(Application Framework)以及应用程序层(Application)。 在组织上,本书将上述内容划分为初识Android系统、Android专用驱动系统和Android应用程序框架三大章。初识Android系统介绍了参考书籍、基础知识以及实验环境搭建;Android专用驱动系统介绍了Logger日志驱动程序、Binder进程间通信驱动程序以及Ashmem匿名共享内存驱动程序;Android应用程序框架从组件、进程、消息以及安装四个维度来对Android应用程序的框架进行了深入的剖析。 通过上述内容及其组织,本书能使读者既能从整体上把握Android系统的层次结构,又能从细节上去掌握每个层次的要点。 作者简介 · · · · · · 罗升阳,1984年出生,2007年毕业于浙江大学计算机系,取得学士学位,2010年毕业于上海交通大学计算机系,取得硕士学位。毕业后直从事于互联网软件开发,并且致力于移动平台的研究,特别是对Android平台有深入的理解和研究。在国内知名IT技术社区CSDN上发表了数十高质量的Android系统原创性文章,并且开设博客专栏--《老罗的Android之旅》,积极与网友互动,深受大家喜爱,访问量直居于前茅。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值