Binder机制——AndroidStudio简单实现进程间通信

本文详细介绍通过Binder机制实现Android跨进程通信的过程。包括服务端创建AIDL接口、定义服务类,以及客户端如何连接并调用服务端的方法。

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

服务端:

启动AS,创建一个服务端工程,在工程中点击右键New->AIDL->AIDL File


我们打开这个aidl文件,在其中添加一个测试方法:

此时编译工程,在如下路径自动生成一个aidl对应的java文件(该java类的代码就是整个Binder机制的原理所在):


创建一个服务类,继承Service:
package sources.cn.binderserviceprj;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2018/1/24.
 */

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myService;
    }

    private IBinder myService = new IMyAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public int add(int num1, int num2) throws RemoteException {
            Log.d("test","==num1==>" + num1 + ", ====num2===>" + num2);
            return num1 + num2;
        }
    };
}

配置文件中配置该Service:
<!--exported:允许外界访问该服务,AIDL必备条件-->
        <service android:name=".MyService"
            android:exported="true"/>
最后主程序中启动该服务:
public class MainActivity extends AppCompatActivity {

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

        startService(new Intent(this,MyService.class));

    }
}

客户端:

在工程中点击右键New->Module


进入工程所在的目录,将服务端的aidl目录完全拷贝到客户端对应的目录下:


同样,此时需要编译一次客户端代码,好了此时编写客户端代码:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/binderBtn"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>
MainActivity.java
package sources.cn.binderclient;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import sources.cn.binderserviceprj.IMyAidlInterface;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    IMyAidlInterface iMyAidlInterface;
    private Button binderBtn;

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

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("sources.cn.binderserviceprj","sources.cn.binderserviceprj.MyService"));
        bindService(intent,conn,BIND_AUTO_CREATE);

        binderBtn = (Button) findViewById(R.id.binderBtn);
        binderBtn.setOnClickListener(this);
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            iMyAidlInterface = null;
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    @Override
    public void onClick(View view) {
        try{
            int result = iMyAidlInterface.add(1,2);
            Log.d("test","=======result=====" + result);
        }catch (RemoteException e){
            e.printStackTrace();
        }
    }
}

测试结果:



至此,简单实现进程间通信








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值