Cause Analysis of broadcast message exceptions caused by Intent transmission (reverse inference of Android source code is analyzed)

Source: Internet
Author: User

The Music application is responsible for playing Music. If a Music still has images, You need to display the images. When the Music played by the Music application changes, you need to broadcast messages, notify B of the name and image of the music being played through broadcast messages. If Song 1 is played, application B can normally receive the name and image of Song 1, but if it switches to Song 2, application B cannot receive the broadcast message. The code for sending a broadcast message in the Music application is as follows: [java] System. out. println ("play" + "send intent, musicTitle =" + getTrackName () + "musicAlbum" + getAlbumName (); Intent intent Intent = new intent (); intent. setAction ("com. zhao3546.ACTION _ MUSIC_DETAIL "); intent. putExtra ("musicTitle", getTrackName (); intent. putExtra ("musicAlbum", getAlbumName (); Bitmap bmp = MusicUtils. getArtwork (this. getApplicationContext (), getAudioId (), getAlbum Id (); intent. putExtra ("musicImage", bmp); System. out. println ("bmp. getByteCount () = "+ bmp. getByteCount (); sendBroadcast (intent); the Code for dynamically registering and receiving broadcast messages in application B is as follows: [java] IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction ("com. zhao3546.ACTION _ MUSIC_DETAIL "); registerReceiver (mReceiver, intentFilter); [java] private class MusicDetailReceiver extends BroadcastReceiver {@ Override public void OnReceive (Context context, Intent intent) {final String title = intent. getStringExtra ("musicTitle"); final String album = intent. getStringExtra ("musicAlbum"); System. out. println ("musicdetailpolicer. onReceiver "+" title = "+ title +" album = "+ album); final Bitmap bmp = (Bitmap) intent. getParcelableExtra ("musicImage"); play Song 1. The output log is as follows: [plain] 09-11 16:12:16. 732: I/System. out (14177): play send int Ent, musicTitle = recent hot transformers simple mechanical short message sound musicalbumwww.w.hao.net 09-11 16:12:16. 776: I/System. -out (14177): bmp. getByteCount () = 367236 09-11 16:12:16. 802: I/System. out (13265): musicdetailpolicer. onReceiver title = recently popular transformers simple mechanical short message sound effect album = www.w.hao.net Playing Song 2. The output log is as follows: [java] 09-11 16:12:25. 266: I/System. out (14177): play send intent, musicTitle = Bach-Goldberg Variations 1 2 3 musicAlbumClassical 1 09-11 16:12:25. 329: I/System. -out (14177): bmp. getByteCount () = 524288 when using Song 1 for testing, and when using Song 2 for testing, I also modified some code in the middle. At the beginning, I thought about the problem of code modification; however, later I used Song 1 to test and found that the latest code is also very helpful. I analyzed the log carefully and found it in bmp. getByteCount () = 524288 this line of log contains an error log, see the red font: 09-11 16:12:25. 266: I/System. out (14177): play send intent, musicTitle = Bach-Goldberg Variations 1 2 3 musicAlbumClassical 109-11 16:12:25. 329: I/System. -out (14177): bmp. getByteCount () = 52428809-11 16:12:25. 3 42: E/JavaBinder (9985 ):!!! Failed binder transaction !!! The complete Android 4.2.2 source code is available. I searched for the code based on "FAILED BINDER TRANSACTION" and found that it was output in signalExceptionForError OF THE android_util_Binder.cpp class: [cpp] void signalExceptionForError (JNIEnv * env, jobject obj, status_t err, bool canThrowRemoteException) {switch (err ){... case FAILED_TRANSACTION: ALOGE ("!!! Failed binder transaction !!! "); // TransactionTooLargeException is a checked exception, only throw from certain methods. // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION // but it is not the only one. the Binder driver can return BR_FAILED_REPLY // for other reasons also, such as if the transaction is malformed or // refers to an FD that has been closed. we shoshould change the driver // to enable Us to distinguish these cases in the future. jniThrowException (env, canThrowRemoteException? "Android/OS/TransactionTooLargeException": "java/lang/RuntimeException", NULL); break ;... then, based on the keyword "FAILED_TRANSACTION", further search is performed in IPCThreadState. the waitForResponse () method of cpp contains the following code: From the function where this problem occurs, we can know that this broadcast message has been transmitted to the Binder driver layer, waiting for the Binder driver layer to respond. Why? This is a long topic. If you don't know, read "Android framework secrets" or Lao Luo's Blog: "Android inter-process communication (IPC) mechanism Binder brief introduction and learning plan. [Cpp] status_t IPCThreadState: waitForResponse (Parcel * reply, status_t * acquireResult) {int32_t cmd; int32_t err; while (1) {if (err = talkWithDriver ()) <NO_ERROR) break; err = mIn. errorCheck (); if (err <NO_ERROR) break; if (mIn. dataAvail () = 0) continue; cmd = mIn. readInt32 (); IF_LOG_COMMANDS () {alog <"Processing waitForResponse Command:" <getReturnString (cmd) <endl;} switch (cmd ){. .. Case BR_FAILED_REPLY: err = FAILED_TRANSACTION; goto finish;...} finish: if (err! = NO_ERROR) {if (acquireResult) * acquireResult = err; if (reply) reply-> setError (err); mLastError = err;} return err;} IPCThreadState: waitForResponse () the error code BR_FAILED_REPLY is converted to FAILED_TRANSACTION and thrown to the upper layer. Then, let's look at where the error code BR_FAILED_REPLY is generated. After further searching, we find that the error code is in binder. c (in the Binder driver source code) is frequently used: [cpp] Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ d River \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ d River \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ d River \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ d River \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ d River \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; Binder. c (e: \ git \ android4.2.2 source code \ driver \ binder): return_error = BR_FAILED_REPLY; binder. this class c does not include the full Android source code downloaded in a normal way. For how to download it, refer to another blog: Android Binder driver source code. Further analyze the previous log: the size of the normally transmitted image: 09-11 16:12:16. 776: I/System. -out (14177): bmp. getByteCount () = 367236 the size of the image cannot be transmitted normally: 09-11 16:12:25. 329: I/System. -out (14177): bmp. the image with the getByteCount () = 524288 exception is about 42% larger than the normal image, and then goes reverse to speculate. Since the image is too large, where can this problem occur? When the BR_FAILED_REPLY keyword appears, I have skipped it. I personally think it is the most like in the following section. Due to the large image size, the buffer allocation problem occurs: [cpp] static void binder_transaction (struct binder_proc * proc, struct binder_thread * thread, struct binder_transaction_data * tr, int reply ){... t-> buffer = binder_alloc_buf (target_proc, tr-> data_size, tr-> offsets_size ,! Reply & (t-> flags & TF_ONE_WAY); if (t-> buffer = NULL) {return_error = BR_FAILED_REPLY; goto err_binder_alloc_buf_failed;} Of course, it is only speculation, no further verification is performed. If the root cause of the problem is analyzed, the solution is actually relatively simple: 1. Save the image to the SD card, pass the path to application B, and let application B load the image from the SD card; 2. compress the image before transmitting it. for Android, you can analyze the source code to find the root cause of the problem. To solve a problem, in turn, we can deepen our understanding of Android as a whole.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.