Android details: AndroidRuntimeException: This message is already in use, androidmessage

Source: Internet
Author: User

Android details: AndroidRuntimeException: This message is already in use, androidmessage

Today, when I was working on a project to process the message queue, I encountered such an exception. AndroidRuntimeException: This message is already in use.

My specific business needs at that time were as follows: When I wanted to interact with hardware, I had only one operation within a certain period of time. If I didn't use idle, I would resend the message, in addition, the message should be delay for a period of time, that is, TIMEDELAY.

The specific error code is as follows:

private class ChargecaseServiceHandler extends Handler {public ChargecaseServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);Command command = new Command();command.command = msg.what;if (msg.obj != null) {command.data = (Bundle) msg.obj;}if (sIsLoopBleServiceReady) {if (isIdle) {switch (msg.what) {case MESSAGE_KILL:stopSelf();break;case MESSAGE_START_BLE_SCAN:startBLEScan();break;case MESSAGE_CONNECT:connect(command.data);break;case MESSAGE_DISCONNECT:attemptDisconnect();break;case MESSAGE_RELEASE_DEVICE_ID:clearData();break;case MESSAGE_INITIALIZE:initialize();break;case MESSAGE_RESET_BLE:resetBleController();break;case MESSAGE_STARTUP:startup();break;case MESSAGE_REGISTER:register(command.data);break;case MESSAGE_UNREGISTER:unregister(command.data);break;case MESSAGE_RECONNECT:reconnect();break;case MESSAGE_ADDCARD:addCard(command.data);break;case MESSAGE_GETCARDLIST:getCardList(command.data);// removeMessages(MESSAGE_GETCARDLIST);break;case MESSAGE_GETCARDDETAIL:getCardDetail(command.data);break;case MESSAGE_REMOVECARD:removeCard(command.data);break;case MESSAGE_CHECK_BATTERY:checkBattery();break;case MESSAGE_SET_DEFAULT_CARD:setDefaultCard(command.data);break;case MESSAGE_ZAP_CARD:zapCard(command.data);break;case MESSAGE_SET_LOCK_TIME:setLockTimer(command.data);}} else {sServiceHandler.sendMessageDelayed(msg, TIMEDELAY);}} else {Toast.makeText(getApplicationContext(),"Starting ble for you.", Toast.LENGTH_SHORT).show();}}}

Then I searched the internet and found that the sent message is in the message queue, indicating that the message is currently in use.


Refer to the great god blog, (Great God blog please move to the http://blog.csdn.net/aa4790139/article/details/6579009)


Solution: Create a new message and send it! Solve the problem.


So I prepared a new message or an obtain message, instead of directly sending the message for delay. But the error persists. The code for the second error is as follows:

private class ChargecaseServiceHandler extends Handler {public ChargecaseServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);Command command = new Command();command.command = msg.what;if (msg.obj != null) {command.data = (Bundle) msg.obj;}if (sIsLoopBleServiceReady) {if (isIdle) {switch (msg.what) {case MESSAGE_KILL:stopSelf();break;case MESSAGE_START_BLE_SCAN:startBLEScan();break;case MESSAGE_CONNECT:connect(command.data);break;case MESSAGE_DISCONNECT:attemptDisconnect();break;case MESSAGE_RELEASE_DEVICE_ID:clearData();break;case MESSAGE_INITIALIZE:initialize();break;case MESSAGE_RESET_BLE:resetBleController();break;case MESSAGE_STARTUP:startup();break;case MESSAGE_REGISTER:register(command.data);break;case MESSAGE_UNREGISTER:unregister(command.data);break;case MESSAGE_RECONNECT:reconnect();break;case MESSAGE_ADDCARD:addCard(command.data);break;case MESSAGE_GETCARDLIST:getCardList(command.data);// removeMessages(MESSAGE_GETCARDLIST);break;case MESSAGE_GETCARDDETAIL:getCardDetail(command.data);break;case MESSAGE_REMOVECARD:removeCard(command.data);break;case MESSAGE_CHECK_BATTERY:checkBattery();break;case MESSAGE_SET_DEFAULT_CARD:setDefaultCard(command.data);break;case MESSAGE_ZAP_CARD:zapCard(command.data);break;case MESSAGE_SET_LOCK_TIME:setLockTimer(command.data);}} else {Message newMsg = sServiceHandler.obtainMessage();newMsg = msg;removeMessages(msg.what);LogHelper.i(LogHelper.CHARGECASE_TAG, newMsg.what + "");sServiceHandler.sendMessageDelayed(newMsg, TIMEDELAY);}} else {Toast.makeText(getApplicationContext(),"Starting ble for you.", Toast.LENGTH_SHORT).show();}}}

As a result, I thought about whether newMsg is a new message?

Certainly not. Otherwise there will be no exception. So I changed the writing method so that what and obj of newMsg are equal to what and obj of the original msg, so as to maintain the consistency of the actions passed through handler, and newMsg is brand new.

Sure enough, no exception is reported.


The solution is as follows:

private class ChargecaseServiceHandler extends Handler {public ChargecaseServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);Command command = new Command();command.command = msg.what;if (msg.obj != null) {command.data = (Bundle) msg.obj;}if (sIsLoopBleServiceReady) {if (isIdle) {switch (msg.what) {case MESSAGE_KILL:stopSelf();break;case MESSAGE_START_BLE_SCAN:startBLEScan();break;case MESSAGE_CONNECT:connect(command.data);break;case MESSAGE_DISCONNECT:attemptDisconnect();break;case MESSAGE_RELEASE_DEVICE_ID:clearData();break;case MESSAGE_INITIALIZE:initialize();break;case MESSAGE_RESET_BLE:resetBleController();break;case MESSAGE_STARTUP:startup();break;case MESSAGE_REGISTER:register(command.data);break;case MESSAGE_UNREGISTER:unregister(command.data);break;case MESSAGE_RECONNECT:reconnect();break;case MESSAGE_ADDCARD:addCard(command.data);break;case MESSAGE_GETCARDLIST:getCardList(command.data);// removeMessages(MESSAGE_GETCARDLIST);break;case MESSAGE_GETCARDDETAIL:getCardDetail(command.data);break;case MESSAGE_REMOVECARD:removeCard(command.data);break;case MESSAGE_CHECK_BATTERY:checkBattery();break;case MESSAGE_SET_DEFAULT_CARD:setDefaultCard(command.data);break;case MESSAGE_ZAP_CARD:zapCard(command.data);break;case MESSAGE_SET_LOCK_TIME:setLockTimer(command.data);}} else {Message newMsg = sServiceHandler.obtainMessage();newMsg.what = msg.what;newMsg.obj = msg.obj;removeMessages(msg.what);LogHelper.i(LogHelper.CHARGECASE_TAG, newMsg.what + "");sServiceHandler.sendMessageDelayed(newMsg, TIMEDELAY);}} else {Toast.makeText(getApplicationContext(),"Starting ble for you.", Toast.LENGTH_SHORT).show();}}}


There are many switch messages, which are required by the business. The exception is also classic and exclusive.


PS: on the Internet, many solutions are to replace "new Message ()" with "obtainMessage ()", and some are to replace "obtainMessage ()" with "new Message (). my personal tests cannot solve or solve my problems.

PS2: the problem and solution are in the last else. The purpose of listing other large sections of code is to help you understand the error situation. Please check the official website.

However, this method can certainly solve similar problems.



Android reports error AndroidRuntimeException: This message is already in use. Why? I will add points.

When placed outside the while statement, you only have one Message object, which is sent through sendMessage () and processed in handleMessage (). At this time, your loop is still running, assign a value to the object. The result is that two threads may process an object at the same time and an error occurs.
In the while clause, a message object is re-created at the beginning of each loop. you operate on different objects, so there is no problem.

Android log information is:

Let's take a look at the errors.
......
Log. I ("test", "1 ");
......
Log. I ("test", "2 ");
......
Log. I ("test", "3 ");
......

...... Indicates that there may be wrong code blocks,
You should know the execution sequence of android functions.
 

Related Article

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.