Today, I would like to share my initial thread of feeling, although the wording is slightly rough, and does not use thread threads in the core of the Looper,messagequeue message queue knowledge, because it is a beginner thread, so only with the most basic to write, Learn to optimize for improvement.
The previous section "2 ways to implement thread in Android" describes the 2 implementations of thread in Android, and today I use the latter, by implementing the Runnable interface to implement the thread Refreshthread I refresh the chat message.
|
Private classRefreshthreadImplements runnable{
private int await = 0;
Private Boolean flag = true;
@Override
public void Run () {
while (flag) {
try {
Message message =new message ();
Message.what = 1; handler.sendmessage (message); Thread.Sleep (await ()); } catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
public void Setflag (Boolean flag) {
This.flag = Flag;
} Public long await () {
if (await > 13) {
return 60000L;
}
if (await > 6) {
return 20000L;
}
Return await < 3? 10000l:15000l;
} public void setawait (int await) {
This.await = await;
}
public void ascawait () {
await + +;
} } |
The core of this thread is await () this method, when await this variable <3,thread.sleep () thread Sleep 10s, of course, is our message 10s refresh once;
When (await>3&&await<6), the message 15s refresh once, you can see, await>6, the message 20s refresh once, await>13, the message one minute to refresh. After all that time,
What exactly is await this variable? By the following code, we can see that when we compare the length value of a message list, the value of the list before the refresh and the size of the refresh if it does not change, then our await++,
The corresponding thread is refreshthread.ascawait () This method, which means that if no one has been sent messages, then we will refresh the time is longer, the corresponding refresh time may be 10s,15s,20s, or even a minute; if someone sends a message , if the value of the message list is unequal, we are going to assign the await value to 0 so that it refreshes in time.
|
Case I_getmessageinfo:
Chatmsg chatmsg=chatmsgdata.getchatmsg ();
if (Viewutil.isnotempty (chatmsg)) {
if (Integer.parseint (Chatmsg.getstate (). toString ()) >0)
{ synchronized(this) {
List<chatmsglist> chatmsglist=chatmsg.getchatmsglist (); size=chatmsglists.size (); Chatmsglists.clear ();
Chatmsglists.addall (chatmsglist);
Myadapter.notifydatasetchanged ();
Listview.setselection (Chatmsglists.size ()); if (chatmsglists.size () = = size) {
refreshthread.ascawait ();
}else {
refreshthread.setawait (0);
} }
}
Else
{
Showtoast ("Get message list unsuccessful", false);
}
}
Break |
Of course, the interaction between threads is Handler this mechanism , through Handler to notify the main thread to update the message list, in order to do a message list size comparison.
|
Private Handler Handler = new Handler () {
@Override
public voidHandlemessage(Android.os.Message msg) {
Switch (msg.what) {
Case 1:
Sendtobackgroud (operatecode.i_getmessageinfo,new string[]{personalid, ""});
Break
Default
Break
}
Super.handlemessage (msg);
};
}; |
OK, so this is how I use threads to refresh the understanding and implementation of the message.
Android Chat Interface Refresh message