您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android應(yīng)用程序如何把消息加入到消息隊(duì)列中”,在日常操作中,相信很多人在Android應(yīng)用程序如何把消息加入到消息隊(duì)列中問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android應(yīng)用程序如何把消息加入到消息隊(duì)列中”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
應(yīng)用程序的主線程準(zhǔn)備就好消息隊(duì)列并且進(jìn)入到消息循環(huán)后,其它地方就可以往這個(gè)消息隊(duì)列中發(fā)送消息了。
在Android應(yīng)用程序啟動(dòng)過程源代碼分析這篇文章的Step 30中,ActivityManagerService通過調(diào)用ApplicationThread類的scheduleLaunchActivity函 數(shù)通知應(yīng)用程序。
它可以加載應(yīng)用程序的默認(rèn)Activity了,這個(gè)函數(shù)定義在frameworks/base/core/java/android /app/ActivityThread.java文件中:
[java] view plaincopypublic final class ActivityThread { ...... private final class ApplicationThread extends ApplicationThreadNative { ...... // we use token to identify this activity without having to send the // activity itself back to the activity manager. (matters more with ipc) public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident, ActivityInfo info, Bundle state, List pendingResults, List pendingNewIntents, boolean notResumed, boolean isForward) { ActivityClientRecord r = new ActivityClientRecord(); r.token = token; r.ident = ident; r.intent = intent; r.activityInfo = info; r.state = state; r.pendingResults = pendingResults; r.pendingIntents = pendingNewIntents; r.startsNotResumed = notResumed; r.isForward = isForward; queueOrSendMessage(H.LAUNCH_ACTIVITY, r); } ...... } ...... }
這里把相關(guān)的參數(shù)都封裝成一個(gè)ActivityClientRecord對象r,然后調(diào)用queueOrSendMessage函數(shù)來往應(yīng)用程序的消息隊(duì) 列中加入一個(gè)新的消息(H.LAUNCH_ACTIVITY),這個(gè)函數(shù)定義在frameworks/base/core/java/android /app/ActivityThread.java文件中:
[java] view plaincopypublic final class ActivityThread { ...... private final class ApplicationThread extends ApplicationThreadNative { ...... // if the thread hasn't started yet, we don't have the handler, so just // save the messages until we're ready. private final void queueOrSendMessage(int what, Object obj) { queueOrSendMessage(what, obj, 0, 0); } ...... private final void queueOrSendMessage(int what, Object obj, int arg1, int g2) { synchronized (this) { ...... Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; mH.sendMessage(msg); } } ...... } ...... }
到此,關(guān)于“Android應(yīng)用程序如何把消息加入到消息隊(duì)列中”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。