Android AIDL相关
Android AIDL相关
【好奇驱动学习】AIDL是Android接口定义语言(Android Interface definition language)它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信(IPC)接口,从而满足进程间通信的需求。
创建AIDL文件
在src/main下创建aidl文件夹,在文件夹中创建.aidl文件
在文件中写下业务方法
在Build菜单中选择rebuild project
重新编译项目后,安卓的SDK工具会在build目录生成与之对应的,同名的.java文件
编写布局的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">
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"/>
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑服务"/>
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="text"/>
</LinearLayout>
创建远程服务类
package com.example.aidlapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
public class RemoteDataService extends Service {
public RemoteDataService() {
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private final IMyAidlInterface.Stub binder = new IMyAidlInterface.Stub() {
private String data;
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
this.data = data;
}
@Override
public String getData() throws RemoteException {
return " === data is === " + data + " === process id === " + Process.myPid();
}
};
}
修改MainActivity
package com.example.aidlapplication;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements OnClickListener {
private static final String TAG = "MainActivity";
private IMyAidlInterface iMyAidlInterface;
private TextView textView;
private Button bind;
private Button unbind;
private Boolean isUnbind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
init();
}
private void init() {
textView = findViewById(R.id.text_view);
bind = findViewById(R.id.bind);
unbind = findViewById(R.id.unbind);
bind.setOnClickListener(this);
unbind.setOnClickListener(this);
}
public void bindMyService(View view) {
Log.v(TAG, "==绑定服务==");
Intent intent = new Intent(this, RemoteDataService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
isUnbind = false;
}
public void unbindMyService(View view) {
Log.v(TAG, "==解绑服务==");
// 可多次解绑
if (!isUnbind) {
unbindService(serviceConnection);
textView.setText("服务已解绑");
isUnbind = true;
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
Log.v(TAG, "业务对象==>" + iMyAidlInterface);
try {
iMyAidlInterface.setData("inputData");
String data = iMyAidlInterface.getData();
textView.setText("调用服务:" + data);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "==服务中断调用==");
}
};
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.bind:
bindMyService(v);
break;
case R.id.unbind:
unbindMyService(v);
break;
default:
break;
}
}
}
远程调用
将上面的工程app装载在安卓机器上后,重新创建一个新的APP项目
aidl文件增加
这里将上个APP的文件拷过来,同时保证目录也相同,重新rebuild下项目
xml配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/call_remote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="远程调用"/>
<TextView
android:id="@+id/call_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
MainActivity修改
package com.example.wxbremoteserviceclient;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Service;
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.widget.Button;
import android.widget.TextView;
import com.example.aidlapplication.IMyAidlInterface;
public class MainActivity extends AppCompatActivity {
private Button callBut;
private TextView callView;
private IMyAidlInterface serviceProxy;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取远程服务代理
serviceProxy = IMyAidlInterface.Stub.asInterface(service);
try {
serviceProxy.setData("我是来自远程的client调用");
String data = serviceProxy.getData();
callView.setText(data);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callBut = findViewById(R.id.call_remote);
callView = findViewById(R.id.call_textView);
callBut.setOnClickListener(v -> {
// 跳转的时候intent中需要放this,Activity.class,这里不需要放
Intent intent = new Intent();
//重点:这里必须要加上包的名称
intent.setPackage("com.example.aidlapplication");
intent.setAction("com.example.aidlapplication.REMOTE_SERVICE");
bindService(intent, connection, Service.BIND_AUTO_CREATE);
});
}
}
本文来自博客园,作者:阿寳同學Zybao,转载请注明原文链接:https://www.cnblogs.com/zybao/p/16937512.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步