Get the App
SLTechnology News&Howtos  ›  Development  › 

How Android applications add messages to message queues

Shulou Source: shulou.com Published: 2022-06-02 06:08:00 09月20日 Update

This article mainly introduces "how Android applications add messages to message queues". In daily operations, I believe many people have doubts about how Android applications add messages to message queues. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts of "how Android applications add messages to message queues". Next, please follow the editor to study!

After the main thread of the application prepares the message queue and enters the message loop, other places can send messages to the message queue.

In Step 30 of the article on source code analysis of the Android application startup process, ActivityManagerService notifies the application by calling the scheduleLaunchActivity function of the ApplicationThread class.

It can load the default Activity of the application, which is defined in the frameworks/base/core/java/android / app/ActivityThread.java file:

[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);} }. }

Here, all the relevant parameters are encapsulated into an ActivityClientRecord object r, and then a new message (H.LAUNCH_ACTIVITY) is added to the message queue of the application by calling the queueOrSendMessage function, which is defined in the frameworks/base/core/java/android / app/ActivityThread.java file:

[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);}}. }. At this point, the study on "how Android applications add messages to the message queue" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Messages applications applications queues learning functions files more articles help utility next parameters places good news objects articles methods source code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi macOS Microsoft MariaDB