Android Binder 用法详解

ops/2025/3/4 0:05:33/

Binder 是 Android 系统中的一种进程间通信(IPC)机制,它允许不同进程之间进行高效通信。Binder 在 Android 系统中被广泛使用,例如在 Activity 与 Service 的交互中。

Binder 的基本组成

实现 Binder 通信通常包含以下几个关键部分:

  1. AIDL 接口定义:通过 Android Interface Definition Language 定义接口
  2. 服务端实现:实现 AIDL 接口并在 Service 中提供服务
  3. 客户端调用:绑定服务并调用远程方法

下面通过一个完整的示例来展示 Binder 的使用方法。

示例:创建一个计算器服务

第一步:定义 AIDL 接口

创建文件 ICalculator.aidl

// ICalculator.aidl
package com.example.binderexample;interface ICalculator {int add(int a, int b);int subtract(int a, int b);int multiply(int a, int b);int divide(int a, int b);
}

第二步:创建服务端 Service

// CalculatorService.java
package com.example.binderexample;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;public class CalculatorService extends Service {private static final String TAG = "CalculatorService";// Binder 实现private final ICalculator.Stub mBinder = new ICalculator.Stub() {@Overridepublic int add(int a, int b) throws RemoteException {Log.d(TAG, "add() called with: a = [" + a + "], b = [" + b + "]");return a + b;}@Overridepublic int subtract(int a, int b) throws RemoteException {Log.d(TAG, "subtract() called with: a = [" + a + "], b = [" + b + "]");return a - b;}@Overridepublic int multiply(int a, int b) throws RemoteException {Log.d(TAG, "multiply() called with: a = [" + a + "], b = [" + b + "]");return a * b;}@Overridepublic int divide(int a, int b) throws RemoteException {Log.d(TAG, "divide() called with: a = [" + a + "], b = [" + b + "]");if (b == 0) {throw new RemoteException("Division by zero");}return a / b;}};@Overridepublic IBinder onBind(Intent intent) {Log.d(TAG, "onBind: Service bound");return mBinder;}
}

第三步:在 AndroidManifest.xml 中注册服务

<serviceandroid:name=".CalculatorService"android:enabled="true"android:exported="true"><intent-filter><action android:name="com.example.binderexample.ICalculator" /></intent-filter>
</service>

第四步:客户端实现

// MainActivity.java
package com.example.binderexample;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private EditText mNum1EditText, mNum2EditText;private TextView mResultTextView;private Button mAddButton, mSubtractButton, mMultiplyButton, mDivideButton;private ICalculator mCalculator;private boolean mBound = false;// 服务连接对象private ServiceConnection mConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mCalculator = ICalculator.Stub.asInterface(service);mBound = true;Log.d(TAG, "onServiceConnected: Service connected");Toast.makeText(MainActivity.this, "Calculator Service Connected", Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceDisconnected(ComponentName name) {mCalculator = null;mBound = false;Log.d(TAG, "onServiceDisconnected: Service disconnected");}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化 UI 组件mNum1EditText = findViewById(R.id.editText_num1);mNum2EditText = findViewById(R.id.editText_num2);mResultTextView = findViewById(R.id.textView_result);mAddButton = findViewById(R.id.button_add);mSubtractButton = findViewById(R.id.button_subtract);mMultiplyButton = findViewById(R.id.button_multiply);mDivideButton = findViewById(R.id.button_divide);// 添加按钮点击事件mAddButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {performOperation("add");}});mSubtractButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {performOperation("subtract");}});mMultiplyButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {performOperation("multiply");}});mDivideButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {performOperation("divide");}});}private void performOperation(String operation) {if (!mBound) {Toast.makeText(this, "Service not bound", Toast.LENGTH_SHORT).show();return;}try {// 获取输入值int num1 = Integer.parseInt(mNum1EditText.getText().toString());int num2 = Integer.parseInt(mNum2EditText.getText().toString());int result = 0;// 执行远程操作switch (operation) {case "add":result = mCalculator.add(num1, num2);break;case "subtract":result = mCalculator.subtract(num1, num2);break;case "multiply":result = mCalculator.multiply(num1, num2);break;case "divide":result = mCalculator.divide(num1, num2);break;}// 显示结果mResultTextView.setText("结果: " + result);} catch (RemoteException e) {Log.e(TAG, "RemoteException: " + e.getMessage());Toast.makeText(this, "Remote exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();} catch (NumberFormatException e) {Toast.makeText(this, "Please enter valid numbers", Toast.LENGTH_SHORT).show();}}@Overrideprotected void onStart() {super.onStart();// 绑定服务Intent intent = new Intent();intent.setAction("com.example.binderexample.ICalculator");intent.setPackage(getPackageName());bindService(intent, mConnection, Context.BIND_AUTO_CREATE);}@Overrideprotected void onStop() {super.onStop();// 解绑服务if (mBound) {unbindService(mConnection);mBound = false;}}
}

第五步:布局文件

<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><EditTextandroid:id="@+id/editText_num1"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入第一个数字"android:inputType="number" /><EditTextandroid:id="@+id/editText_num2"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入第二个数字"android:inputType="number" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/button_add"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="+" /><Buttonandroid:id="@+id/button_subtract"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="-" /><Buttonandroid:id="@+id/button_multiply"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="×" /><Buttonandroid:id="@+id/button_divide"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="÷" /></LinearLayout><TextViewandroid:id="@+id/textView_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:text="结果: "android:textSize="18sp" /></LinearLayout>

Binder 工作原理解释

  1. AIDL 编译:编译时,Android 工具会根据 AIDL 文件生成相应的 Java 接口,其中包含一个内部抽象类 Stub,继承自 Binder 并实现了该接口

  2. Stub 类

    • 服务端继承并实现 Stub 类的抽象方法
    • Stub 类拥有 asInterface() 静态方法,用于将 IBinder 转换为接口类型
  3. 通信过程

    • 客户端调用 bindService() 绑定服务
    • 服务端返回 Binder 对象
    • 客户端通过 Stub.asInterface() 将 Binder 对象转换为接口
    • 客户端调用接口方法,实际上是通过 Binder 驱动进行跨进程通信

高级用法:传输复杂对象

如果需要传输复杂对象,需要实现 Parcelable 接口:

// Person.java
package com.example.binderexample;import android.os.Parcel;
import android.os.Parcelable;public class Person implements Parcelable {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}protected Person(Parcel in) {name = in.readString();age = in.readInt();}public String getName() {return name;}public int getAge() {return age;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(age);}@Overridepublic int describeContents() {return 0;}public static final Creator<Person> CREATOR = new Creator<Person>() {@Overridepublic Person createFromParcel(Parcel in) {return new Person(in);}@Overridepublic Person[] newArray(int size) {return new Person[size];}};
}

然后在 AIDL 文件中引用这个类:

// Person.aidl
package com.example.binderexample;
parcelable Person;
// IPersonService.aidl
package com.example.binderexample;import com.example.binderexample.Person;interface IPersonService {void addPerson(in Person person);List<Person> getAllPersons();
}

Android C++ 中的 Binder 使用

在 Android C++ 层也可以使用 Binder 进行进程间通信。实际上,Android 框架中的核心 Binder 实现就是用 C++ 编写的,位于 native 层。C++ 中的 Binder 框架是 Java Binder 框架的基础。

Android Native Binder 主要组件

在 C++ 层使用 Binder 主要涉及以下几个关键类:

  1. IBinder:表示一个 Binder 对象的基类
  2. BpBinder:代理端的 Binder 实现(客户端)
  3. BBinder:本地端的 Binder 实现(服务端)
  4. BnInterface:服务端接口模板类
  5. BpInterface:客户端接口模板类
  6. IInterface:Binder 接口的基类
  7. ProcessState:管理进程的 Binder 状态
  8. IPCThreadState:管理线程的 Binder 状态

C++ Binder 实现示例

下面是一个完整的 C++ Binder 示例,包括服务端和客户端。

第一步:定义接口

首先,我们需要定义一个计算器服务的接口:

// ICalculator.h
#ifndef ICALCULATOR_H
#define ICALCULATOR_H#include <binder/IInterface.h>
#include <binder/Parcel.h>namespace android {// 接口标识符
enum {CALCULATOR_ADD = IBinder::FIRST_CALL_TRANSACTION,CALCULATOR_SUBTRACT,CALCULATOR_MULTIPLY,CALCULATOR_DIVIDE
};// 接口定义
class ICalculator : public IInterface {
public:DECLARE_META_INTERFACE(Calculator); // 声明接口元信息// 纯虚函数,需要子类实现virtual int32_t add(int32_t a, int32_t b) = 0;virtual int32_t subtract(int32_t a, int32_t b) = 0;virtual int32_t multiply(int32_t a, int32_t b) = 0;virtual int32_t divide(int32_t a, int32_t b) = 0;
};// 服务端接口
class BnCalculator : public BnInterface<ICalculator> {
public:// onTransact 函数处理来自客户端的请求virtual status_t onTransact(uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags = 0);
};} // namespace android#endif // ICALCULATOR_H

第二步:实现接口

接下来,我们需要实现这个接口:

// ICalculator.cpp
#include "ICalculator.h"namespace android {// 实现元接口宏
IMPLEMENT_META_INTERFACE(Calculator, "android.calculator.ICalculator");// 处理远程调用请求
status_t BnCalculator::onTransact(uint32_t code, const Parcel& data,Parcel* reply, uint32_t flags) {switch (code) {case CALCULATOR_ADD: {CHECK_INTERFACE(ICalculator, data, reply);int32_t a = data.readInt32();int32_t b = data.readInt32();int32_t result = add(a, b);reply->writeInt32(result);return NO_ERROR;}case CALCULATOR_SUBTRACT: {CHECK_INTERFACE(ICalculator, data, reply);int32_t a = data.readInt32();int32_t b = data.readInt32();int32_t result = subtract(a, b);reply->writeInt32(result);return NO_ERROR;}case CALCULATOR_MULTIPLY: {CHECK_INTERFACE(ICalculator, data, reply);int32_t a = data.readInt32();int32_t b = data.readInt32();int32_t result = multiply(a, b);reply->writeInt32(result);return NO_ERROR;}case CALCULATOR_DIVIDE: {CHECK_INTERFACE(ICalculator, data, reply);int32_t a = data.readInt32();int32_t b = data.readInt32();if (b == 0) {return BAD_VALUE; // 除数为零错误}int32_t result = divide(a, b);reply->writeInt32(result);return NO_ERROR;}default:return BBinder::onTransact(code, data, reply, flags);}
}// 客户端代理实现
class BpCalculator : public BpInterface<ICalculator> {
public:BpCalculator(const sp<IBinder>& impl) : BpInterface<ICalculator>(impl) {}virtual int32_t add(int32_t a, int32_t b) {Parcel data, reply;data.writeInterfaceToken(ICalculator::getInterfaceDescriptor());data.writeInt32(a);data.writeInt32(b);remote()->transact(CALCULATOR_ADD, data, &reply);return reply.readInt32();}virtual int32_t subtract(int32_t a, int32_t b) {Parcel data, reply;data.writeInterfaceToken(ICalculator::getInterfaceDescriptor());data.writeInt32(a);data.writeInt32(b);remote()->transact(CALCULATOR_SUBTRACT, data, &reply);return reply.readInt32();}virtual int32_t multiply(int32_t a, int32_t b) {Parcel data, reply;data.writeInterfaceToken(ICalculator::getInterfaceDescriptor());data.writeInt32(a);data.writeInt32(b);remote()->transact(CALCULATOR_MULTIPLY, data, &reply);return reply.readInt32();}virtual int32_t divide(int32_t a, int32_t b) {Parcel data, reply;data.writeInterfaceToken(ICalculator::getInterfaceDescriptor());data.writeInt32(a);data.writeInt32(b);status_t status = remote()->transact(CALCULATOR_DIVIDE, data, &reply);if (status != NO_ERROR) {return -1; // 错误处理}return reply.readInt32();}
};} // namespace android

第三步:实现服务端

服务端需要实现 ICalculator 接口,并提供一个服务:

// CalculatorService.h
#ifndef CALCULATOR_SERVICE_H
#define CALCULATOR_SERVICE_H#include "ICalculator.h"namespace android {class CalculatorService : public BnCalculator {
public:static void instantiate(); // 用于注册服务// 实现ICalculator接口的方法virtual int32_t add(int32_t a, int32_t b);virtual int32_t subtract(int32_t a, int32_t b);virtual int32_t multiply(int32_t a, int32_t b);virtual int32_t divide(int32_t a, int32_t b);
};} // namespace android#endif // CALCULATOR_SERVICE_H
// CalculatorService.cpp
#include "CalculatorService.h"
#include <binder/IServiceManager.h>
#include <utils/Log.h>namespace android {void CalculatorService::instantiate() {sp<IServiceManager> sm = defaultServiceManager();sm->addService(String16("calculator"), new CalculatorService());ALOGI("Calculator service started");
}int32_t CalculatorService::add(int32_t a, int32_t b) {ALOGI("add() called with: a = %d, b = %d", a, b);return a + b;
}int32_t CalculatorService::subtract(int32_t a, int32_t b) {ALOGI("subtract() called with: a = %d, b = %d", a, b);return a - b;
}int32_t CalculatorService::multiply(int32_t a, int32_t b) {ALOGI("multiply() called with: a = %d, b = %d", a, b);return a * b;
}int32_t CalculatorService::divide(int32_t a, int32_t b) {ALOGI("divide() called with: a = %d, b = %d", a, b);return a / b;
}} // namespace android

第四步:服务端主程序

编写服务端主程序,用于启动服务:

// server_main.cpp
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include "CalculatorService.h"using namespace android;int main() {sp<ProcessState> proc(ProcessState::self());// 实例化并注册服务CalculatorService::instantiate();// 启动线程池处理请求ProcessState::self()->startThreadPool();IPCThreadState::self()->joinThreadPool();return 0;
}

第五步:客户端实现

客户端程序示例:

// client_main.cpp
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <iostream>
#include "ICalculator.h"using namespace android;
using namespace std;int main() {sp<IServiceManager> sm = defaultServiceManager();// 获取计算器服务sp<IBinder> binder = sm->getService(String16("calculator"));if (binder == nullptr) {ALOGE("Failed to get calculator service");return -1;}// 创建代理对象sp<ICalculator> calculator = interface_cast<ICalculator>(binder);// 使用计算器服务int a = 10, b = 5;cout << "Addition: " << a << " + " << b << " = " << calculator->add(a, b) << endl;cout << "Subtraction: " << a << " - " << b << " = " << calculator->subtract(a, b) << endl;cout << "Multiplication: " << a << " * " << b << " = " << calculator->multiply(a, b) << endl;cout << "Division: " << a << " / " << b << " = " << calculator->divide(a, b) << endl;return 0;
}

编译和构建

在 Android Native 代码中,通常会在 Android.bpAndroid.mk 文件中配置编译规则:

// Android.bp
cc_binary {name: "calculator_service",srcs: ["ICalculator.cpp","CalculatorService.cpp","server_main.cpp",],shared_libs: ["libutils","libbinder","liblog",],
}cc_binary {name: "calculator_client",srcs: ["ICalculator.cpp","client_main.cpp",],shared_libs: ["libutils","libbinder","liblog",],
}

Native Binder 与 Java Binder 的区别

C++ Binder 和 Java Binder 的主要区别:

  1. API 差异

    • C++ 使用 BBinder、BpBinder、BnInterface 等类
    • Java 使用 Binder、BinderProxy、IInterface.Stub 等类
  2. 内存管理

    • C++ 使用 sp<>(强指针)进行引用计数
    • Java 依赖于 JVM 的垃圾回收
  3. 接口定义

    • C++ 需要手动定义和实现接口类和代理类
    • Java 使用 AIDL 自动生成接口代码
  4. 错误处理

    • C++ 使用状态码(status_t)
    • Java 使用异常机制(RemoteException)

与 C++ 服务绑定的 Java 客户端

Java 应用也可以通过 JNI 调用 C++ 层的 Binder 服务:

// 在 Java 中获取本地服务
public class NativeBinderHelper {static {System.loadLibrary("nativebinderhelper");}// 本地方法声明public static native int addNumbers(int a, int b);public static native int subtractNumbers(int a, int b);
}
// JNI 实现
#include <jni.h>
#include <binder/IServiceManager.h>
#include "ICalculator.h"using namespace android;// JNI 函数实现
extern "C" JNIEXPORT jint JNICALL
Java_com_example_NativeBinderHelper_addNumbers(JNIEnv* env, jclass clazz, jint a, jint b) {sp<IServiceManager> sm = defaultServiceManager();sp<IBinder> binder = sm->getService(String16("calculator"));if (binder == nullptr) {return -1;}sp<ICalculator> calculator = interface_cast<ICalculator>(binder);return calculator->add(a, b);
}extern "C" JNIEXPORT jint JNICALL
Java_com_example_NativeBinderHelper_subtractNumbers(JNIEnv* env, jclass clazz, jint a, jint b) {sp<IServiceManager> sm = defaultServiceManager();sp<IBinder> binder = sm->getService(String16("calculator"));if (binder == nullptr) {return -1;}sp<ICalculator> calculator = interface_cast<ICalculator>(binder);return calculator->subtract(a, b);
}

实际应用场景

C++ Binder 在 Android 系统中广泛应用于:

  1. 系统服务:如 SurfaceFlinger、AudioFlinger 等系统服务
  2. HAL 接口:硬件抽象层接口通常使用 Binder
  3. 高性能需求:需要高性能 IPC 的场景

总结

Android C++ 层的 Binder 机制是 Android IPC 系统的基础,它提供了:

  1. 完整的 IPC 框架:支持跨进程方法调用
  2. 类型安全:通过接口定义保证类型安全
  3. 高性能:直接在 native 层实现,避免了 JNI 开销
  4. 安全性:支持身份验证和访问控制

虽然 C++ Binder 的使用相对 Java Binder 更复杂,需要手动实现更多代码,但它在性能和系统集成方面具有优势,特别适合系统级服务和对性能要求高的应用场景。


http://www.ppmy.cn/ops/162905.html

相关文章

如何在netlify一键部署静态网站

1. 准备你的项目 确保你的静态网站文件&#xff08;如 HTML、CSS、JavaScript、图片等&#xff09;都在一个文件夹中。通常&#xff0c;项目结构如下&#xff1a; my-static-site/ ├── index.html ├── styles/ │ └── styles.css └── scripts/└── script.js…

分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测

分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测 目录 分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测分类效果基本介绍程序设计参考资料 分类效果 基本介绍 1.Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机…

矩阵的奇异值(SVD)分解和线性变换

矩阵的奇异值&#xff08;SVD&#xff09;分解和线性变换 SVD定义 奇异值分解&#xff08;Singular Value Decomposition&#xff0c;简称 SVD&#xff09;是一种重要的线性代数工具&#xff0c;能够将任意矩阵 ( A ∈ R m n \mathbf{A} \in \mathbb{R}^{m \times n} A∈Rmn…

ubuntu中ollama设置记录

自己同一台电脑主机安装3080和3090显卡&#xff0c;测试发现ollama只默认跑在3090上&#xff1b;故查看一下设置&#xff0c;成功也把3080也运行起来了。 原因如下&#xff1a; 开始设置记录&#xff1a; Environment Variables: OLLAMA_DEBUG 作用&#xff1a;显示额外的调试…

【R语言】PCA主成分分析

使用R语言手动实现PCA主成分分析计算&#xff0c;通过计算协方差矩阵计算出数据的主成分得分&#xff0c;根据的分最高的特征进行得分图的绘制 # 读取数据raw_data <- read.csv("R可视化/data.csv", header TRUE, fileEncoding "GBK")new_data <-…

【C】链式二叉树算法题1 -- 单值二叉树

leetcode链接https://leetcode.cn/problems/univalued-binary-tree/description/ 1 题目描述 如果二叉树每个节点都具有相同的值&#xff0c;那么该二叉树就是单值二叉树。只有给定的树是单值二叉树时&#xff0c;才返回 true&#xff1b;否则返回 false。 示例 1&#xff1…

大模型微调入门(Transformers + Pytorch)

目标 输入&#xff1a;你是谁&#xff1f; 输出&#xff1a;我们预训练的名字。 训练 为了性能好下载小参数模型&#xff0c;普通机器都能运行。 下载模型 # 方式1&#xff1a;使用魔搭社区SDK 下载 # down_deepseek.py from modelscope import snapshot_download model_…

Android AsyncLayoutInflater异步加载xml布局文件,Kotlin

Android AsyncLayoutInflater异步加载xml布局文件&#xff0c;Kotlin implementation "androidx.asynclayoutinflater:asynclayoutinflater:1.1.0-alpha01" import android.os.Bundle import android.util.Log import android.view.View import android.view.ViewGro…