這篇文章主要介紹“Android應(yīng)用程序如何把消息加入到消息隊(duì)列中”,在日常操作中,相信很多人在Android應(yīng)用程序如何把消息加入到消息隊(duì)列中問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android應(yīng)用程序如何把消息加入到消息隊(duì)列中”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)東至免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過(guò)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
應(yīng)用程序的主線程準(zhǔn)備就好消息隊(duì)列并且進(jìn)入到消息循環(huán)后,其它地方就可以往這個(gè)消息隊(duì)列中發(fā)送消息了。
在Android應(yīng)用程序啟動(dòng)過(guò)程源代碼分析這篇文章的Step 30中,ActivityManagerService通過(guò)調(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對(duì)象r,然后調(diào)用queueOrSendMessage函數(shù)來(lái)往應(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í),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!