其他分享
首页 > 其他分享> > AIDL入门学习二,android组件化

AIDL入门学习二,android组件化

作者:互联网

public Person(String name, int age) {

this.name = name;

this.age = age;

}

protected Person(Parcel in) {

name = in.readString();

age = in.readInt();

}

public static final Creator CREATOR = new Creator() {

@Override

public Person createFromParcel(Parcel in) {

return new Person(in);

}

@Override

public Person[] newArray(int size) {

return new Person[size];

}

};

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return ag

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

e;

}

public void setAge(int age) {

this.age = age;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel parcel, int i) {

parcel.writeString(name);

parcel.writeInt(age);

}

@Override

public String toString() {

return “Person{” +

“name=’” + name + ‘’’ +

“, age=” + age +

‘}’;

}

}

2、客户端

=====

public class ClientActivity extends AppCompatActivity {

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);

bindService();

findViewById(R.id.btn_click).setOnClickListener(view -> {

try {

List persons = iImoocAidl.add(new Person(“李四”, 18));

Log.e(“hehe”, persons.toString());

} catch (RemoteException e) {

e.printStackTrace();

}

});

}

private void bindService() {

// 获取服务端

Intent intent = new Intent();

intent.setComponent(new ComponentName(“com.test.aidltest3”,

“com.test.aidltest3.IRemoteService”));

bindService(intent, mConn, BIND_AUTO_CREATE);

}

@Override

protected void onDestroy() {

super.onDestroy();

unbindService(mConn);

}

}

布局文件:

<?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">

标签:name,AIDL,Person,age,Override,void,组件,android,public
来源: https://blog.csdn.net/m0_64603776/article/details/122167205