听宇哥讲Binder(Binder核心原理最全解析)

本文深入探讨了AIDL的工作原理及其与Binder机制的关系。通过分析AIDL接口定义语言及生成代码,揭示了进程间通信的实现细节。进一步讨论了Binder机制如何优化进程间通信,包括其性能优势和稳定性特点。

前面写了一篇关于AIDL的文章,那我们就从AIDL谈起吧。如果对AIDL有不理解的,可以先看看这一篇文章: 

再探AIDL_AD钙奶-lalala的博客-优快云博客

我们还是延续前面一篇文章的例子来讲解,我们创建的AIDL文件如下:

// IMyAidlInterface.aidl
package com.example.aidl;

// Declare any non-default types here with import statements

interface IMyAidlInterface {

   String getUserName();

   String getPassword();

}

然后Build->Make Project,会自动在build->generated->aidl_source_output_dir里面生成固定格式的文件。想了解Binder的原理,就先来了解一下AIDL吧。

生成的文件如下:

/*
 * This file is auto-generated.  DO NOT MODIFY.
 */
package com.example.aidl;
// Declare any non-default types here with import statements

public interface IMyAidlInterface extends android.os.IInterface
{
  /** Default implementation for IMyAidlInterface. */
  public static class Default implements com.example.aidl.IMyAidlInterface
  {
    @Override public java.lang.String getUserName() throws android.os.RemoteException
    {
      return null;
    }
    @Override public java.lang.String getPassword() throws android.os.RemoteException
    {
      return null;
    }
    @Override
    public android.os.IBinder asBinder() {
      return null;
    }
  }
  /** Local-side IPC implementation stub class. */
  public static abstract class Stub extends android.os.Binder implements com.example.aidl.IMyAidlInterface
  {
    private static final java.lang.String DESCRIPTOR = "com.example.aidl.IMyAidlInterface";
    /** Construct the stub at attach it to the interface. */
    public Stub()
    {
      this.attachInterface(this, DESCRIPTOR);
    }
    /**
     * Cast an IBinder object into an com.example.aidl.IMyAidlInterface interface,
     * generating a proxy if needed.
     */
    public static com.example.aidl.IMyAidlInterface asInterface(android.os.IBinder obj)
    {
      if ((obj==null)) {
        return null;
      }
      android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
      if (((iin!=null)&&(iin instanceof com.example.aidl.IMyA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是宇哥啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值