Service进程间通信

一.AIDL简介

AIDL,全称是Android Interface Define Language,即安卓接口定义语言,可以实现安卓设备中进程之间的通信(Inter Process Communication, IPC)。

安卓中的服务分为2类:本地服务 和 远程服务

二.AIDL使用

假设需要计算两个数的和,服务端提供方法,客户端提供两个数的值。
通过该案例模拟Service间的进程通信

三.具体实现步骤

1.服务端

首先要创建aidl文件
1.创建包,取名为aidl
2.在包下new->AIDL->AIDL File

在这里插入图片描述
在该文件中创建要使用的方法
然后需要Build项目

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

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

interface IMyAidlInterface {
    int add(int a,int b);
}

Acitvity中没有任何东西

创建Service
在Service中声明IBinder代理人
IMyAidlInterface中的Stub

public static abstract class Stub extends android.os.Binder implements com.example.day3_6.IMyAidlInterface

Stub继承自Binder
而Binder实现了IBinder

public class Binder implements IBinder

所以可以直接声明使用

public class MyService extends Service {
    public MyService() {
    }

    IBinder binder = new IMyAidlInterface.Stub() {
        @Override
        public int add(int a, int b) throws RemoteException {
            return a + b;
        }
    };

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

然后在清单文件中注册我们的Service并设置Action

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.xzk.app36"/>
    </intent-filter>
</service>

服务端就到此完成

2.客户端

在客户端创建同样AIDL文件,要求包名AIDL名字必须一致,内容也必须一样提供add方法,然后再重新Build项目

Activity布局中放置一个按钮作为触发

<Button
    android:id="@+id/but_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="调用服务端的add方法" />

再在Activity中动态绑定Service

通过Intent设置包和Action作为标识

onServiceConnected方法里将IBinder实例对象转化为IMyAidlInterface

这样就可以调用IMyAidlInterface中的方法了

public class MainActivity extends AppCompatActivity {
    private Button butAdd;
    private ServiceConnection serviceConnection;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        butAdd = (Button) findViewById(R.id.but_add);

        butAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setPackage("com.example.day3_6");
                intent.setAction("com.xzk.app36");
                serviceConnection = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        IMyAidlInterface binder = IMyAidlInterface.Stub.asInterface(service);
                        try {
                            int add = binder.add(3, 2);
                            Toast.makeText(MainActivity.this, "" + add, Toast.LENGTH_SHORT).show();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {

                    }
                };
                //绑定Service
                bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除绑定
        unbindService(serviceConnection);
    }
}

3.效果展示

在这里插入图片描述

三.特别注意

在运行项目时,一定要先运行玩服务端后再运行客户端,否则找不到包

要开心加油

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值