使用AIDL传递自定义Bean结构,必须实现parcelable接口
Server中进行定义Bean结构
public class User implements Parcelable { private String userName; private String passwd; public User() { } public User(String userName, String passwd) { this.userName = userName; this.passwd = passwd; } protected User(Parcel in) { userName = in.readString(); passwd = in.readString(); } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(userName); dest.writeString(passwd); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } @Override public String toString() { return "User{" + "userName='" + userName + '\'' + ", passwd='" + passwd + '\'' + '}'; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
定义AIDL接口文件
package cn.edu.hebust.aidldemo2;import cn.edu.hebust.aidldemo2.User;interface IGetUser { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); List<User> getUserInfo(String name);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
定义AIDLBean文件
这里必须定义AIDL Bean文件,不然会出现import error 的编译错误,将AIDL声明Bean结构即可
package cn.edu.hebust.aidldemo2;import cn.edu.hebust.aidldemo2.User;parcelable User;
Ctrl+B 编译这个项目
这时会生成相应的Java文件, 然后使用这个java文件进行通信
提供一个Service供通信
public class MyService extends Service { private IBinder mStub = new IGetUser.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public List<User> getUserInfo(String name) throws RemoteException { List<User> list = new LinkedList<>(); for (int i = 0; i < 10; i++) { list.add(new User(name + i, name + (i >> 2))); } return list; } }; @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return mStub; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
client的配置
拷贝相同的aidl文件和bean文件
注意Bean文件的包名应该一样
public class MainActivity extends AppCompatActivity { private static final String TAG = "LOGGER"; private TextView mTvText; private ServiceConnection mConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { IGetUser mStub = IGetUser.Stub.asInterface(service); try { List<User> list = mStub.getUserInfo("terry"); for (User aUser : list) { mTvText.append(aUser.toString() + "\n"); } } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected: "); Toast.makeText(MainActivity.this, "onServiceDisconnected", Toast.LENGTH_SHORT).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvText = (TextView) findViewById(R.id.id_tv_text); Intent intent = new Intent(); ComponentName componentName = new ComponentName("cn.edu.hebust.aidldemo2" , "cn.edu.hebust.aidldemo2.MyService"); intent.setComponent(componentName); bindService(intent, mConn, BIND_AUTO_CREATE); } @Override protected void onDestroy() { unbindService(mConn); super.onDestroy(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47