大佬教程收集整理的这篇文章主要介绍了AIDL入门学习一,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
视频:AIDL-小白成长记
文章:Android AIDL使用详解
官方文档:Android 接口定义语言 (AIDL)
以下摘抄自官方文档:
Android 接口定义语言 (AIDL) 与您可能使用过的其他接口语言 (IDL) 类似。您可以利用它定义客户端与服务均认可的编程接口,以便二者使用进程间通信 (IPC) 进行相互通信。在 Android 中,一个进程通常无法访问另一个进程的内存。因此,为进行通信,进程需将其对象分解成可供操作系统理解的原语,并将其编组为可供您操作的对象。编写执行该编组操作的代码较为繁琐,因此 Android 会使用 AIDL 为您处理此问题。
注意:只有在需要不同应用的客户端通过 IPC 方式访问服务,并且希望在服务中进行多线程处理时,您才有必要使用 AIDL。如果您无需跨不同应用执行并发 IPC,则应通过实现 Binder 来创建接口;或者,如果您想执行 IPC,但不需要处理多线程,请使用 Messenger 来实现接口。无论如何,在实现 AIDL 之前,请您务必理解绑定服务。
在开始设计 AIDL 接口之前,请注意,AIDL 接口的调用是直接函数调用。您无需对发生调用的线程做任何假设。实际情况的差异取决于调用是来自本地进程中的线程,还是远程进程中的线程。具体而言:
oneway
关键字用于修改远程调用的行为。使用此关键字后,远程调用不会屏蔽,而只是发送事务数据并立即返回。最终接收该数据时,接口的实现会将其视为来自 Binder
线程池的常规调用(普通的远程调用)。如果 oneway
用于本地调用,则不会有任何影响,且调用仍为同步调用。AIDL案例: 定义AIDL文件 实现服务端 实现客户端
思路图:
// IImoocAidl.aidl
package com.test.server;
// Declare any non-default types here with import statements
interface IImoocAidl {
// 计算两个数的和
int add(int num1,int num2);
}
然后make project,会生成IImoocAidl.java文件
项目目录:
public class IRemoteService extends Service {
private static final String TAG = "IRemoteService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
private IBinder iBinder = new IImoocAidl.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
Log.e(TAG, "收到了远程的请求,请求的参数是:num1 = " + num1 + "num2 = " + num2);
return num1 + num2;
}
};
}
public class IRemoteService extends Service {
private static final String TAG = "IRemoteService";
/**
* 当客户端绑定到该服务的时候,会执行此方法
* @param intent
* @return
*/
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
private IBinder iBinder = new IImoocAidl.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
Log.e(TAG, "收到了远程的请求,请求的参数是:num1 = " + num1 + "num2 = " + num2);
return num1 + num2;
}
};
}
配置文件注册:
<service
android:name=".IRemoteService"
android:exported="true" />
启动Service:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, IRemoteService.class);
startService(intent);
}
}
在同项目中新建一个名为client的module
ClientActivity
public class ClientActivity extends AppCompatActivity {
EditText etNum1, etNum2;
TextView tvRes;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
etNum1 = findViewById(R.id.et_num1);
etNum2 = findViewById(R.id.et_num2);
tvRes = findViewById(R.id.tv_res);
btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ClientActivity">
<EditText
android:id="@+id/et_num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入第一个数字"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="100sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_num1" />
<EditText
android:id="@+id/et_num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入第二个数字"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_add" />
<TextView
android:id="@+id/tv_res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="计算结果"
android:textSize="40sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_num2" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="计算"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_res" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class ClientActivity extends AppCompatActivity {
private static final String TAG = "ClientActivity";
EditText etNum1, etNum2;
TextView tvRes;
Button btnAdd;
IImoocAidl iImoocAidl;
private ServiceConnection mConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// 拿到了远程服务
iImoocAidl = IImoocAidl.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// 回收资源
iImoocAidl = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
etNum1 = findViewById(R.id.et_num1);
etNum2 = findViewById(R.id.et_num2);
tvRes = findViewById(R.id.tv_res);
btnAdd = findViewById(R.id.btn_add);
bindService();
btnAdd.setOnClickListener(view -> {
int num1 = Integer.parseInt(etNum1.getText().toString());
int num2 = Integer.parseInt(etNum2.getText().toString());
try {
// 调用远程服务
if (null != iImoocAidl) {
int res = iImoocAidl.add(num1, num2);
tvRes.setText("计算结果为:" + res);
} else {
Log.e(TAG, "iImoocAidl is null!");
}
} catch (RemoteException e) {
e.printStackTrace();
tvRes.setText("Error!");
}
});
}
private void bindService() {
// 获取服务端
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.test.server",
"com.test.server.IRemoteService"));
bindService(intent, mConn, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mConn);
}
}
以上是大佬教程为你收集整理的AIDL入门学习一全部内容,希望文章能够帮你解决AIDL入门学习一所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。