进程通信组件-AIDL的使用

本文详细介绍了AIDL(Android Interface Definition Language),一种用于实现不同Android进程间数据通信的接口。从AIDL组件的创建、服务端实现、本地及远程客户端的使用等方面进行阐述,展示了如何创建AIDL接口,以及如何在Service中返回接口并实现自定义方法。通过AIDL,本地和远程客户端可以获取到接口对象,从而进行进程间的数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AIDL:Android interface definition language 接口定义语言

不同APP进程之间数据通信的接口

目录

(一)AIDL组件的创建

  • 创建AIDL组件
  • 在生成的AIDL接口内部 申明 需要自定义实现的方法

(二)服务端

  • 在Service的 Binder() 对象的实例中,返回AIDL的接口,并且实现自定义 

(三)本地客户端

  •  实现 ServiceConnection 接口,在其中拿到 AIDL 类   

  • IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象

  •  通过返回的IMyAidlInterface对象,拿到自定义的方法

(四)远程客户端

  • 保持和本地客户端的AIDL完全一致(直接将本地客户端main文件夹下的aidl文件夹拷贝到远程客户端main文件夹下)Make project ,生成对应的 Binder 的 Java 文件

  •  在远程客户端实现service绑定操作,实现 ServiceConnection 接口,在其中拿到 AIDL 类 (同本地客户端一样操作)

  • IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象

  • 通过返回的IMyAidlInterface对象,拿到自定义的方法,拿到不同进程需要传递的数据

(一)AIDL组件的创建

  • 创建AIDL组件

  • 在生成的AIDL接口内部 申明 需要自定义实现的方法

      IMyAidlInterface.aidl

// IMyAidlInterface.aidl
package com.moxuan.test;

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

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float 
                    aFloat,double aDouble, String aString);//系统默认生成的方法
    void getProgess();//自定义方法
}
  • Make project ,生成 Binder 的 Java 文件,可以在app→build→generated→source→aidl文件夹下找到对应的AIDL java文件

(二)服务端

  • 在Service的 Binder() 对象的实例中,返回AIDL的接口,并且实现自定义 

(三)本地客户端

  •  在MainActivity中实现 ServiceConnection 接口,在其中拿到 AIDL 类   

    ServiceConnection用于绑定客户端和服务 执行服务的绑定操作时ServiceConnection()会被调用

      执行服务的绑定操作,当客户端正常连接服务时,onServiceConnected()会被调用,进而AIDL接口会被调用

  • IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象

 

public class MainActivity extends AppCompatActivity {
    private TextView abd;
    private TextView abd1;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

    private  ServiceConnection serviceConnection= new ServiceConnection() {//用于绑定客户端和服务
        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {//当客户端正常连接服务时,执行服务的绑定操作会被调用
            IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                aidl.getProgess();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {//当客户端正常连接丢失时调用
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        abd = (TextView) findViewById(R.id.abd);
        abd1 = (TextView) findViewById(R.id.abd1);
        /*
        服务启动
         */
      Intent intent=new Intent(MainActivity.this,MyService.class);
        startService(intent);

     //  bindService(intent,serviceConnection ,BIND_AUTO_CREATE);

        //绑定
        abd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection ,BIND_AUTO_CREATE);
            }
        });
        abd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //解绑
               unbindService(serviceConnection);
            //   IBinder:用于远程操作的接口
            // ServiceConnection;
                
            }
        });

    }
}
  •  通过返回的IMyAidlInterface对象,拿到自定义的方法

(四)远程客户端

  • 保持和本地客户端的AIDL完全一致(直接将本地客户端main文件夹下的aidl文件夹拷贝到远程客户端main文件夹下)Make project ,生成对应的 Binder 的 Java 文件
  •  在远程客户端实现service绑定操作,在activity中实现 ServiceConnection 接口,在其中拿到 AIDL 类 (同本地客户端一样操作)(参考三、本地客户端)

ServiceConnection用于绑定客户端和服务 执行服务的绑定操作时ServiceConnection()会被调用


执行服务的绑定操作,当客户端正常连接服务时,onServiceConnected()会被调用,进而AIDL接口会被调用

  • IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象

  • 通过返回的IMyAidlInterface对象,拿到自定义的方法,拿到不同进程需要传递的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值