看代码目录
服务端代码
// DNAIdl.aidl
package com.dn_alan.service;
// Declare any non-default types here with import statements
import com.dn_alan.service.Person;
interface DNAIdl {
void addPerson(in Person person);
List<Person> getPersonList();
}
// DNAIdl.aidl
package com.dn_alan.service;
// Declare any non-default types here with import statements
parcelable Person;
package com.dn_alan.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.List;
public class DNAidlService extends Service {
private ArrayList<Person> personArrayList;
@Override
public IBinder onBind(Intent intent) {
personArrayList = new ArrayList<>();
return iBinder;
}
private IBinder iBinder = new DNAIdl.Stub() {
@Override
public void addPerson(Person person) throws RemoteException {
personArrayList.add(person);
}
@Override
public List<Person> getPersonList() throws RemoteException {
return personArrayList;
}
};
}
package com.dn_alan.service;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, DNAidlService.class));
}
}
package com.dn_alan.service;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name;
private int grade;
protected Person(Parcel in) {
name = in.readString();
grade = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(grade);
}
}
<?xml version="1.0" encoding="utf-8"?>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".DNAidlService"
android:exported="true"/>
</application>
```
客户端代码
// DNAIdl.aidl
package com.dn_alan.service;
// Declare any non-default types here with import statements
import com.dn_alan.service.Person;
interface DNAIdl {
void addPerson(in Person person);
List getPersonList();
}
// DNAIdl.aidl
package com.dn_alan.service;
// Declare any non-default types here with import statements
parcelable Person;
package com.dn_alan.myapplication;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.dn_alan.service.DNAIdl;
import com.dn_alan.service.Person;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private DNAIdl dnaIdl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService();
}
private void bindService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.dn_alan.service",
"com.dn_alan.service.DNAidlService"));
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
dnaIdl = DNAIdl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void click(View view) {
try {
dnaIdl.addPerson(new Person("dn", 10));
List<Person> people = dnaIdl.getPersonList();
Toast.makeText(this, people.toString(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
package com.dn_alan.service;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name;
private int grade;
public Person(String name, int grade) {
this.name = name;
this.grade = grade;
}
public Person(Parcel in) {
name = in.readString();
grade = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(grade);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", grade=" + grade +
'}';
}
}
来源:CSDN
作者:qczg_wxg
链接:https://blog.csdn.net/qczg_wxg/article/details/104155061