1. 先创建要交互的bean类,实现 Parcelable 接口
package com. miss. protobufdemo ;
import android. os. Parcel ;
import android. os. Parcelable ;
public class Book implements Parcelable {
private String name;
private int price;
protected Book ( Parcel in) {
name = in. readString ( ) ;
price = in. readInt ( ) ;
}
public static final Creator < Book > CREATOR = new Creator < Book > ( ) {
@Override
public Book createFromParcel ( Parcel in) {
return new Book ( in) ;
}
@Override
public Book [ ] newArray ( int size) {
return new Book [ size] ;
}
} ;
public Book ( String name, int price) {
this . name = name;
this . price = price;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getPrice ( ) {
return price;
}
public void setPrice ( int price) {
this . price = price;
}
@Override
public int describeContents ( ) {
return 0 ;
}
@Override
public void writeToParcel ( Parcel dest, int flags) {
dest. writeString ( name) ;
dest. writeInt ( price) ;
}
@Override
public String toString ( ) {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}' ;
}
}
2. 创建AIDl文件类
package com. miss. protobufdemo ;
parcelable Book ;
parcelable Person ;
package com. miss. protobufdemo ;
import com. miss. protobufdemo. Book ;
interface BookManager {
void addBook ( in Book book) ;
List < Book > getBooks ( ) ;
void addPerson ( in Person person) ;
}
3. 创建服务端
< service android: name= ".AIDLService"
android: exported= "true"
android: process= ":tpc_service" >
< intent- filter>
< action android: name= "com.miss.protobufdemo" / >
< category android: name= "android.intent.category.DEFAULT" / >
< / intent- filter>
< / service>
public class AIDLService extends Service {
public final String TAG = this . getClass ( ) . getSimpleName ( ) + " tpc " ;
private List < Book > bookList = new ArrayList < > ( ) ;
private final BookManager. Stub stub = new BookManager. Stub ( ) {
@Override
public void addBook ( Book book) throws RemoteException {
if ( book != null ) {
bookList. add ( book) ;
}
Log . e ( TAG, " addBook " ) ;
}
@Override
public List < Book > getBooks ( ) throws RemoteException {
return bookList;
}
@Override
public void addPerson ( Person person) throws RemoteException {
Log . e ( TAG, "addPerson: " + person. toString ( ) ) ;
}
} ;
@Nullable
@Override
public IBinder onBind ( Intent intent) {
return stub;
}
}
4. 创建客户端
public class AIDLActivity extends AppCompatActivity {
private final String TAG = "AIDLActivity tpc " ;
private BookManager mBookManager;
private List < Book > bookList = new ArrayList < > ( ) ;
private static int count = 1 ;
private Boolean mBound = false ;
@Override
protected void onCreate ( @Nullable Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
findViewById ( R . id. btn) . setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
if ( mBound == false ) {
Log . e ( TAG, "onClick: 未连接,尝试连接" ) ;
attemptToBindService ( ) ;
return ;
}
if ( mBookManager == null ) {
return ;
}
try {
mBookManager. addBook ( new Book ( "aaa" , ++ count) ) ;
bookList = mBookManager. getBooks ( ) ;
for ( int i = 0 ; i < bookList. size ( ) ; i++ ) {
Log . e ( TAG, "onClick: " + bookList. get ( i) . toString ( ) ) ;
}
mBookManager. addPerson ( new Person ( "aaa" , 18 ) ) ;
} catch ( RemoteException e) {
e. printStackTrace ( ) ;
}
}
} ) ;
}
private void attemptToBindService ( ) {
Intent intent = new Intent ( this , AIDLService . class ) ;
intent. setAction ( "com.miss.protobufdemo" ) ;
bindService ( intent, mServiceConnection, Context . BIND_AUTO_CREATE) ;
Log . e ( TAG, "---- 尝试 连接 ---- " ) ;
}
private ServiceConnection mServiceConnection = new ServiceConnection ( ) {
@Override
public void onServiceConnected ( ComponentName name, IBinder service) {
Log . e ( TAG, "service connected mBound = true" ) ;
mBookManager = BookManager. Stub . asInterface ( service) ;
mBound = true ;
}
@Override
public void onServiceDisconnected ( ComponentName name) {
Log . e ( TAG, "service disconnected" ) ;
mBound = false ;
Log . e ( TAG, "service connected mBound = false" ) ;
}
} ;
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
if ( mBound) {
unbindService ( mServiceConnection) ;
}
}
}