AIDL简单的使用

AIDL:Android Interface Definition Language,即Android接口定义语言
主要用于进程间的通信

首先是本地调用服务:
1.定义要暴露出来的接口方法:

public interface Iservice {
    public void callControlTemp();
    public void callControlPower();
}

2.完成普通服务的创建:

public class ServiceTest extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public void controlTemp(){
        System.out.println("温控控制!!!");
    }

    public void controlPower(){
        System.out.println("电源控制!!!");
    }

    public class MyBinder extends IService{
        @Override
        public void callControlTemp() {
            controlTemp();
        }

        public void callControlPower(){
            controlPower();
        }
    }
}

3.完成本地Activity

public class MainActivity extends Activity {
    private MyConn conn;
    private Iservice binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, servicetest.class);
        conn = new MyConn();
        startService(intent);
        bindService(intent, conn, BIND_AUTO_CREATE);
    }  

    public class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (IService)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    public void clickPower(View v){
        try {
            binder.callControlPower();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void clickTemp(View v){
        try {
            binder.callControlTemp();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } 

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(conn != null){
            unbindService(conn);
        }
    }
}

输出结果,本地服务调用成功

=============================================================
接下来修改下代码,将本地服务改为远程服务端;
1.将要暴露的接口文件IService.java改为IService.aidl;
2.去掉IService.aidl中的所有修饰符(piublic);
3.修改服务的MyBinder继承自动生成的Stub类(注:此时MyBinder要实现的就是远程服务暴露的方法);

public class servicetest extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public void controlTemp(){
        System.out.println("温控控制!!!");
    }

    public void controlPower(){
        System.out.println("电源控制!!!");
    }

    public class MyBinder extends Stub{
        @Override
        public void callControlTemp() {
            controlTemp();
        }

        @Override
        public void callControlPower(){
            controlPower();
        }
    }
}

4.修改AndroidMainfest.xml;

 <service android:name="com.example.servicetest.servicetest" >
            <intent-filter >
                <action android:name="com.zyw.service"/>
            </intent-filter>
 </service>

5.在远程客户端中开启服务;

然后完成本地调用客户端的实现:
1.新建一个包,包名为IService.aidl所在的包名,然后将IService.aidl复制到这个包中;
2.通过Stub类中的asInterface方法,将service返回至client的对象转换为IService.Stub;

public class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

3.完成本地Activity代码;

public class MainActivity extends Activity {
    private MyConn conn;
    private Iservice binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setAction("com.zyw.service");
        conn = new MyConn();
        bindService(intent, conn, BIND_AUTO_CREATE);
    }  

    public class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    public void clickPower(View v){
        try {
            binder.callControlPower();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void clickTemp(View v){
        try {
            binder.callControlTemp();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } 

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(conn != null){
            unbindService(conn);
        }
    }
}

本地调用远程服务结果:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AIDL(Android Interface Definition Language)是一种用于 Android 平台的接口定义语言,它可以帮助不同进程之间的组件进行通信。下面是使用 AIDL 进行进程间通信的步骤: 1.定义 AIDL 接口 首先,在服务端和客户端之间定义一个 AIDL 接口。在 AIDL 文件中,定义需要向客户端公开的方法和参数。 2.实现 AIDL 接口 在服务端中,实现定义的 AIDL 接口,并在 onCreate() 方法中将其注册到系统中。 3.绑定服务端 在客户端中,使用 bindService() 方法绑定服务端。 4.获取 AIDL 接口实例 在客户端中,实现 ServiceConnection 接口,当服务端连接成功时,会回调 onServiceConnected() 方法。在此方法中,可以获取到 AIDL 接口实例。 5.调用 AIDL 接口方法 在客户端中,通过获取到的 AIDL 接口实例,即可调用服务端暴露的方法。 下面是一个简单的示例代码: 服务端: ``` //定义 AIDL 接口 interface IMyAidlInterface { int add(int a, int b); } //实现 AIDL 接口 class MyAidlInterfaceImpl extends IMyAidlInterface.Stub { @Override public int add(int a, int b) throws RemoteException { return a + b; } } //在 onCreate() 方法中注册 AIDL 接口 @Override public void onCreate() { super.onCreate(); Intent intent = new Intent(this, MyAidlInterfaceService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); Log.i(TAG, "MyAidlInterfaceService is created."); } //定义 ServiceConnection 对象,以便在客户端连接时获取 AIDL 接口实例 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); Log.i(TAG, "MyAidlInterfaceService is connected."); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; Log.i(TAG, "MyAidlInterfaceService is disconnected."); } }; ``` 客户端: ``` //定义 ServiceConnection 对象 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //获取 AIDL 接口实例 mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); Log.i(TAG, "MyAidlInterfaceService is connected."); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; Log.i(TAG, "MyAidlInterfaceService is disconnected."); } }; //使用 bindService() 方法绑定服务端 Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.myaidlservice", "com.example.myaidlservice.MyAidlInterfaceService")); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); //调用服务端暴露的方法 int result = mIMyAidlInterface.add(1, 2); ``` 希望这个简单的示例可以帮助你了解如何使用 AIDL 进行进程间通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值