Android IPC機(jī)制Messenger實(shí)例詳解
創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比克什克騰網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式克什克騰網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋克什克騰地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴(lài)。
前言:
Messenger可以翻譯成信使,通過(guò)它可以在不同進(jìn)程間傳遞Message對(duì)象有了它就可以輕松實(shí)現(xiàn)進(jìn)程間的數(shù)據(jù)傳遞了。
Messenger使用的方法相對(duì)AIDL比較簡(jiǎn)單,它對(duì)AIDL做了一層封裝是的我們不需要像采用AIDL那樣去實(shí)現(xiàn)進(jìn)程通信那么麻煩,可以看看他的源碼有AIDL的跡象。
public final class Messenger implements Parcelable { private final IMessenger mTarget; public Messenger(Handler target) { mTarget = target.getIMessenger(); } public void send(Message message) throws RemoteException { mTarget.send(message); } public IBinder getBinder() { return mTarget.asBinder(); } public boolean equals(Object otherObj) { if (otherObj == null) { return false; } try { return mTarget.asBinder().equals(((Messenger)otherObj) .mTarget.asBinder()); } catch (ClassCastException e) { } return false; } public int hashCode() { return mTarget.asBinder().hashCode(); } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeStrongBinder(mTarget.asBinder()); } public static final Parcelable.CreatorCREATOR = new Parcelable.Creator () { public Messenger createFromParcel(Parcel in) { IBinder target = in.readStrongBinder(); return target != null ? new Messenger(target) : null; } public Messenger[] newArray(int size) { return new Messenger[size]; } }; public static void writeMessengerOrNullToParcel(Messenger messenger, Parcel out) { out.writeStrongBinder(messenger != null ? messenger.mTarget.asBinder() : null); } public static Messenger readMessengerOrNullFromParcel(Parcel in) { IBinder b = in.readStrongBinder(); return b != null ? new Messenger(b) : null; } public Messenger(IBinder target) { mTarget = IMessenger.Stub.asInterface(target); } }
首先我們需要新建一個(gè)Service來(lái)處理客戶(hù)端的請(qǐng)求,同時(shí)聲明一個(gè)Handler作為參數(shù)來(lái)創(chuàng)建一個(gè)Messenger,然后通過(guò)getBinder()方法返回Binder。
public class MessageService extends Service { private Messenger mMessenger = new Messenger(new Handler() { @Override public void handleMessage(Message msgFromClient) { super.handleMessage(msgFromClient); Message msgToTarget = Message.obtain(msgFromClient); msgToTarget.what = 0; try { Thread.sleep(2000); msgToTarget.arg1 = msgFromClient.arg1 + msgFromClient.arg2; msgFromClient.replyTo.send(msgToTarget); } catch (InterruptedException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }); @Nullable @Override public IBinder onBind(Intent intent) { return mMessenger.getBinder(); } }
里面的邏輯是簡(jiǎn)單的將客戶(hù)端傳來(lái)的Message中的arg1和arg2的值相加并將結(jié)果返回給Message對(duì)應(yīng)的replyTo這個(gè)Messenger,并通過(guò)send將服務(wù)端的Message返回給客戶(hù)端。
然后在客戶(hù)端處理:首先需要bindService來(lái)綁定這個(gè)Service,然后通過(guò)IBinder生成一個(gè)Messenger對(duì)象,這個(gè)Messenger對(duì)象就可以將需要處理的數(shù)據(jù)封裝到Message然后send到Service去。
Messenger mMessenger = new Messenger(new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.w("Jayuchou", "--- 從異步線(xiàn)程中讀取到數(shù)據(jù) --- " + msg.arg1); } }); Messenger mService; ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = new Messenger(service); Log.w("Jayuchou", "-- Connected success --"); } @Override public void onServiceDisconnected(ComponentName name) { Log.w("Jayuchou", "-- Connected dismiss --"); mService = null; } };
然后調(diào)用的地方方式為:
Message msgFromClient = Message.obtain(null, 0, 1, 2); msgFromClient.replyTo = mMessenger; try { mService.send(msgFromClient); } catch (RemoteException e) { e.printStackTrace(); }
將數(shù)據(jù)封裝Message中,并且Message中的replyTo指定服務(wù)端中要將結(jié)果回調(diào)的Messenger對(duì)象。
msgFromClient.replyTo.send(msgToTarget);
我們可以看到Service中有這么一句代碼,其中的replyTo就是我們?cè)诳蛻?hù)端傳進(jìn)去的Messenger,這時(shí)候調(diào)用send方法就可以將服務(wù)端的也就是另一個(gè)進(jìn)程的數(shù)據(jù)傳到想要用的進(jìn)程然后采用Messenger進(jìn)行接收,我們可以跟Handler用法類(lèi)似的使用即可。Messenger是一個(gè)輕量級(jí)的AIDL,一次一個(gè)處理請(qǐng)求。
以上就是Android messenger 的消息處理的詳解,關(guān)于Android 開(kāi)發(fā)的文章,本站還很多,請(qǐng)大家搜索參閱,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!