【文件属性】:
文件名称:AIDL进程通信
文件大小:37.67MB
文件格式:RAR
更新时间:2020-09-27 07:53:43
AIDL
android 进程通信,分为service端和client端,通过AIDL 实现service与client端的同信问题,代码如下
public class BookManagerService extends Service {
private String TAG = "BookManagerService";
private NotificationManager mNM;
private CopyOnWriteArrayList bookList= new CopyOnWriteArrayList<>();
private Binder binder = new IBookManager.Stub(){
@Override
public List getBookList() throws RemoteException {
return bookList;
}
@Override
public void addBook(Book book) throws RemoteException {
if(!bookList.contains(book))
bookList.add(book);
Log.i(TAG, "addBook: 添加成功 !");
Log.i(TAG, "addBook: \n book id :"+book.bookId +" \nname : "+book.bookName);
}
@Override
public boolean removeBook(Book book) throws RemoteException {
Log.i(TAG, "removeBook: "+book.toString());
if(remove(bookList,book)){
Log.i(TAG, "removeBook: 删除成功 ");
return true ;
}
Log.i(TAG, "removeBook: 删除失败了 ");
return false ;
}
};
public boolean remove(CopyOnWriteArrayList list ,Book rBook){
for(Book book : list){
if(book.bookName .equals(rBook.bookName) && book.bookId == rBook.bookId){
list.remove(book);
return true ;
}
}
return false ;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: -------- ·");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return binder;
}
}