private CopyOnWriteArrayList<IOnNewBookArrivedListener> mListenerList = new CopyOnWriteArrayList<IOnNewBookArrivedListener>();
private Binder mBinder = new BookManagerImpl(){
@Override
public List<Book> getBookList() throws RemoteException {
Log.e(TAG, "getBookList-->"+ System.currentTimeMillis());
return mBookList;
}
@Override
public void addBook(Book book) throws RemoteException {
Log.e(TAG, "addBook-->");
mBookList.add(book);
}
@Override
public void registerListener(IOnNewBookArrivedListener listener) {
if (!mListenerList.contains(listener)) {
mListenerList.add(listener);
}else {
Log.e(TAG, "already exists");
}
Log.e(TAG, "registerListener, size:"+mListenerList.size());
}
@Override
public void unRegisterListener(IOnNewBookArrivedListener listener) {
if (mListenerList.contains(listener)) {
mListenerList.remove(listener);
Log.e(TAG, "unRegisterListener listener succeed");
}else {
Log.e(TAG, "not found, can not unregister");
}
Log.e(TAG, "unRegisterListener, current size:"+mListenerList.size());
}
};
複製代碼
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate-->"+ System.currentTimeMillis());
mBookList.add(new Book(1, "Android"));
mBookList.add(new Book(2, "IOS"));
new Thread(new ServiceWorker()).start();
}
private void onNewBookArrived(Book book) throws RemoteException{
mBookList.add(book);
Log.e(TAG, "new book arrived, notify listeners:" + mListenerList.size());
for (int i=0; i<mListenerList.size(); i++) {
IOnNewBookArrivedListener listener = mListenerList.get(i);
Log.e(TAG, "new book arrived, notify listener:" + listener);
listener.onNewBookArrived(book);
}
}
private class ServiceWorker implements Runnable {
@Override
public void run() {
while (!mIsServiceDestoryed.get()) {
try {
Thread.sleep(5000);
}catch (InterruptedException e) {
e.printStackTrace();
}
int bookId = mBookList.size() + 1;
Book newBook = new Book(bookId, "new book#" + bookId);
try {
onNewBookArrived(newBook);
}catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
複製代碼
private IBookManager mRemoteBookManager;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e(TAG, "ServiceConnection-->"+ System.currentTimeMillis());
IBookManager bookManager = BookManagerImpl.asInterface(iBinder);
mRemoteBookManager = bookManager;
try {
List<Book> list = bookManager.getBookList();
Log.e(TAG, "query book list, list type:" + list.getClass().getCanonicalName());
Log.e(TAG, "query book list:" + list.toString());
Book newBook = new Book(3, "Android 進階");
bookManager.addBook(newBook);
Log.e(TAG, "add book:" + newBook);
List<Book> newList = bookManager.getBookList();
Log.e(TAG, "query book list:" + newList.toString());
bookManager.registerListener(mOnNewBookArrivedListener);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mRemoteBookManager = null;
Log.e(TAG, "binder died");
}
};
/**
* 這個方法運行在客戶端的binder線程池中,不能直接進行UI操做
*/
private IOnNewBookArrivedListener mOnNewBookArrivedListener = new OnNewBookArrivedListenerImpl(){
@Override
public void onNewBookArrived(Book book) {
mHandler.obtainMessage(MESSAGE_NEW_BOOK_ARRIVED, book).sendToTarget();
}
};
複製代碼
private static final int MESSAGE_NEW_BOOK_ARRIVED = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case MESSAGE_NEW_BOOK_ARRIVED:
Log.e(TAG, "receive new book:" + message.obj);
break;
default:
super.handleMessage(message);
}
}
};
複製代碼
@Override
protected void onDestroy() {
if (mRemoteBookManager != null && mRemoteBookManager.asBinder().isBinderAlive()) {
try {
Log.e(TAG, "unRegister listener:" + mOnNewBookArrivedListener);
mRemoteBookManager.unRegisterListener(mOnNewBookArrivedListener);
}catch (RemoteException e) {
e.printStackTrace();
}
}
unbindService(mConnection);
super.onDestroy();
}
複製代碼
經過上圖咱們能夠看到,咱們已經成功實現了預期的功能,而且服務端通知客戶端的調用過程也如咱們上面所說的那樣bash
接下去咱們退出應用,這樣能夠測試解綁監聽的功能 微信
從上圖咱們能夠看到,服務端調用解綁失敗了,提示找不到接口,這是咋回事呢?cookie
public class RemoteCallbackList<E extends IInterface> {
/*package*/ ArrayMap<IBinder, Callback> mCallbacks
= new ArrayMap<IBinder, Callback>();
...
}
複製代碼
public boolean register(E callback, Object cookie) {
synchronized (mCallbacks) {
if (mKilled) {
return false;
}
IBinder binder = callback.asBinder();
try {
Callback cb = new Callback(callback, cookie);
binder.linkToDeath(cb, 0);
mCallbacks.put(binder, cb);
return true;
} catch (RemoteException e) {
return false;
}
}
}
複製代碼
private RemoteCallbackList<IOnNewBookArrivedListener> mListenerList = new RemoteCallbackList<IOnNewBookArrivedListener>();
private Binder mBinder = new BookManagerImpl(){
@Override
public List<Book> getBookList() throws RemoteException {
Log.e(TAG, "getBookList-->"+ System.currentTimeMillis());
return mBookList;
}
@Override
public void addBook(Book book) throws RemoteException {
Log.e(TAG, "addBook-->");
mBookList.add(book);
}
@Override
public void registerListener(IOnNewBookArrivedListener listener) {
//註冊接口
mListenerList.register(listener);
}
@Override
public void unRegisterListener(IOnNewBookArrivedListener listener) {
//解註冊接口
mListenerList.unregister(listener);
}
};
//通知客戶端
private void onNewBookArrived(Book book) throws RemoteException{
mBookList.add(book);
final int N = mListenerList.beginBroadcast();
for (int i=0; i<N; i++) {
IOnNewBookArrivedListener listener = mListenerList.getBroadcastItem(i);
if (listener != null) {
try {
listener.onNewBookArrived(book);
}catch (RemoteException e) {
e.printStackTrace();
}
}
}
mListenerList.finishBroadcast();
}
複製代碼
歡迎關注個人微信公衆號,和我一塊兒學習一塊兒成長!
複製代碼