AIDL![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f2d456fa666dc98fdfb905753fcffaa.png)
案例需求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/59038bc94601f5cfa137aa3667ff0c93.png)
服务端代码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fb2887872821c4bbfca5c49a75749114.png)
package com.example.service_remote_aidl;
import com.example.service_remote_aidl.Student;
interface IStudentService {
Student getStudentById(int id);
}
package com.example.service_remote_aidl;
parcelable Student;
package com.example.service_remote_aidl;
import android.os.Parcel;
import android.os.Parcelable;
public class Student implements Parcelable {
private int id;
private String name;
private double price;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
public Student(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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;
}
public static Creator<Student> getCREATOR() {
return CREATOR;
}
protected Student(Parcel in) {
id = in.readInt();
name = in.readString();
price = in.readDouble();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeDouble(price);
}
@Override
public int describeContents() {
return 0;
}
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];
}
};
}
package com.example.service_remote_aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyRemoteService extends Service {
public MyRemoteService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.e("TAG", "onBind()");
return new StudentService();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("TAG", "onUnbind()");
return super.onUnbind(intent);
}
class StudentService extends IStudentService.Stub {
@Override
public Student getStudentById(int id) throws RemoteException {
Log.e("TAG", "Service getStudentById() " + id);
return new Student(id, "Tom", 10000);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service_remote_aidl">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Service_remote_aidl">
<service
android:name=".MyRemoteService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.service_remote_aidl.MyRemoteService.Action" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
客户端代码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d4f7d42d30107c1b8d0da71dbc20e131.png)
package com.example.service_client;
import androidx.appcompat.app.AppCompatActivity;
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.EditText;
import android.widget.Toast;
import com.example.service_remote_aidl.IStudentService;
import com.example.service_remote_aidl.Student;
public class MainActivity extends AppCompatActivity {
private EditText et_aidl_id;
private ServiceConnection conn;
private IStudentService studentService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_aidl_id = findViewById(R.id.et_aidl_id);
}
public void bindRemoteService(View view) {
Intent intent = new Intent();
intent.setAction("com.example.service_remote_aidl.MyRemoteService.Action");
intent.setPackage("com.example.service_remote_aidl");
if (conn == null) {
conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
Log.e("TAG", "onServiceConnected()");
studentService = IStudentService.Stub.asInterface(service);
}
};
bindService(intent, conn, Context.BIND_AUTO_CREATE);
Toast.makeText(this, "绑定Service", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "已经绑定Service", Toast.LENGTH_SHORT).show();
}
}
public void invokeRemote(View view) throws RemoteException {
if (studentService != null) {
int id = Integer.parseInt(et_aidl_id.getText().toString());
Student student = studentService.getStudentById(id);
Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
}
}
public void unbindRemoteService(View view) {
if (conn != null) {
unbindService(conn);
conn = null;
studentService = null;
Toast.makeText(this, "解绑Service", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "还未绑定Service", Toast.LENGTH_SHORT).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.service_client">
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<queries>
<package android:name="com.example.service_remote_aidl"></package>
</queries>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Service_remote_aidl">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>