一、AS环境支持
在Android中加入AIDL支持,在build.gradle.kts中加入 aidl = true; 如下:
// build.gradle.kts 中 在buildTypes 下加入
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
buildFeatures{
aidl = true
}
二 创建服务端文件创建
1)service文件和注册
1.1 创建service文件
// MyRemoteService.java
public class MyRemoteService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("TAG","MyRemoteService -> onBind()");
return new StudentService();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG","MyRemoteService -> onUnbind()");
return super.onUnbind(intent);
}
class StudentService extends IStudentAIDLService.Stub {
@Override
public Student getStudentById(int id) throws RemoteException {
Log.e("TAG","MyRemoteService -> StudentService -> getStudentById() id = " + id);
return new Student(id,"TOM",10000);
}
}
}
1.2 service文件注册
在服务端AndroidManifest.xml文件中添加如下代码:
<service
android:name=".remoteservice.MyRemoteService" #MyRemoteService.java
android:enabled="true"
android:exported="true"> #外部可以访问
<intent-filter>
<action android:name="com.XXX.MyRemoteService" /> #定义一个action
</intent-filter>
</service>
2)创建Bean文件(Student.java)
此处创建bean文件应该在Java文件夹下非aidl文件夹下。
public class Student implements Parcelable {
private int id;
private String name;
private double price;
public Student(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
protected Student(Parcel in) {
id = in.readInt();
name = in.readString();
price = in.readDouble();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeDouble(price);
}
}
3)创建aidl文件
1)创建AIDL文件夹


按照上述图创建即可。
2)创建AIDL文件


// IStudentAIDLService.aidl
package com.xxx.xxxx.remoteservice;
import com.xxx.xxxx.remoteservice.Student;
interface IStudentAIDLService {
Student getStudentById(int id);
}
// Student.aidl
package com.xxx.xxxx.remoteservice;
parcelable Student;
创建完成aidl相关文件后,会在java(generated)中生成一个文件,如图:

至此服务端带AIDL的Service创建完成。
三、客户端调用service(bindService)
1 在于服务端相同命名空间中创建bean文件和aidl文件,如果无法创建aidl文件根据as环境配置配置as环境。
具体文件结构如下:

2 配置客户端AndroidManifest.xml
在此文件中加入访问权限,没有加入访问权限将无法访问MyRemoteService。
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<queries>
<package android:name="com.xxx.xxxx"/>
</queries>
3 绑定服务器service
public class MainActivity extends AppCompatActivity {
private EditText et_aidl_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
et_aidl_id = findViewById(R.id.et_aidl_id);
}
private ServiceConnection conn;
private IStudentAIDLService studentAIDLService;
public void bindRemoteService(View view){
if (conn == null){
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("TAG","bindRemoteService() -> onServiceConnected()");
studentAIDLService = IStudentAIDLService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Intent intent = new Intent("com.xxx.MyRemoteService");
intent.setAction("com.XXX.MyRemoteService");
intent.setPackage("com.XXX.l07_service");
boolean bindService = bindService(intent, conn, Context.BIND_AUTO_CREATE);
Log.e("TAG","bindRemoteService() 绑定远程service " + bindService);
}else{
Log.e("TAG","bindRemoteService() 已经绑定远程service");
}
}
public void invokeRemote(View view) throws RemoteException {
if(studentAIDLService != null){
int id = Integer.parseInt(et_aidl_id.getText().toString());
Student student = studentAIDLService.getStudentById(id);
Toast.makeText(this,student.toString(), Toast.LENGTH_SHORT).show();
}
}
public void unbindRemoteService(View view){
if (conn!=null){
unbindService(conn);
studentAIDLService = null;
conn = null;
Log.e("TAG","unbindRemoteService() 解除绑定远程service");
}else{
Log.e("TAG","unbindRemoteService() 已经解除绑定远程service");
}
}
}
至此完成所有创建工作。
548

被折叠的 条评论
为什么被折叠?



