Today, when I was doing a project handling Message Queuing, I encountered a problem, an exception. Androidruntimeexception:this message is already in use.
I was in the context of specific business needs, to be connected with the hardware, the time to keep only one operation, if not idle, resend the message, and this message should delay a period of time, that is timedelay.
The code for the specific 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 {sservicehandler.sendmessagedelayed (msg, timedelay);}} else {Toast.maketext (Getapplicationcontext (), "starting ble for you.", Toast.length_short). Show ();}}
Then search the Internet, and found that the message is actually sent in the message queue, indicating that it is now being used.
After referring to the great God's blog, (please visit the http://blog.csdn.net/aa4790139/article/details/6579009 blog)
Find the solution: Workaround recreate a new message, send past OK! Problem solving.
So I'm going to make a new message or obtain a message instead of just send this message for delay. However, there are still errors, and the code for two errors 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 ();}}
So I have a thought, so I have to newmsg is not a new message it?
Certainly not, otherwise it will be no exception. So change the wording, let Newmsg's what and obj equal to the original msg what and obj, so as to maintain the consistency of the action passed through handler, and newmsg of the new nature.
Sure enough, no longer reported abnormal.
Here's how to fix it:
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 ();}}
Switch message is a lot of business needs, the exception is also very classic, special record.
PS: Many of the solutions on the web are to replace the new message () with Obtainmessage (), or replace the Obtainmessage () with the new message (). Personal testing can't be solved, or I can't solve my problem.
PS2: The problem and problem solving is in the penultimate second else. The purpose of pasting other large pieces of relevant code is to make it easy for you to understand the error situation. Please crossing not to blame.
But my approach is sure to solve similar problems.