1 Description
Multithreading is a common practice in Android development. After multithreading is completed, the UI must be updated. In this article, I will sort it out and attach the Framework Code to help you.
2 Content
Main code structure:
Activity Code File
Processthread code file
In the activity code, the handler msghandler object is created and the handlemessage method is overloaded.
The constructor in processthread needs to input the msghandler object as the constructor parameter. After processing the data, send the message. Handlemessage in the main activity receives the message.
3. project code
Main activity code:
package com.jouhu.androidthreaduihandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class AndroidThreadUIHandlerActivity extends Activity {
public static final int RECV_DATA = 1;
Button btn = null;
ProcessThread pThread = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pThread = new ProcessThread(msgHandler);
}
});
}
private final Handler msgHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case RECV_DATA:
if(pThread != null)
{
byte[] data = pThread.getOneBuffer();
UpdateUI(data);
}
break;
}
}
private void UpdateUI(byte[] data) {
// TODO Auto-generated method stub
}
};
}
Thread code model:
Package com. jouhu. androidthreaduihandler;
Import java. util. arraylist;
Import Android. OS. Handler;
Import Android. OS. message;
Public class processthread extends thread {
Private Static final int buffer size = 1024;
Private handler;
Private arraylist byteslist = NULL;
Boolean running = true;
Public processthread (handler)
{
This. Handler = handler;
Byteslist = new arraylist ();
}
Public void run ()
{
While (running)
{
//
Byte [] buff = new byte [buff_size];
Recv (buff, buff_size );
Message MSG = handler. obtainmessage (androidthreaduihandleractivity. recv_data );
MSG. OBJ = "Recv ";
MSG. sendtotarget ();
}
}
Private void Recv (byte [] buff, int buffsize ){
// Todo auto-generated method stub
// Can process the data received by the socket
}
Public void addonebuffer (byte [] data)
{
// Thread Synchronization
Synchronized (byteslist)
{
Byteslist. Add (data );
}
}
Public byte [] getonebuffer ()
{
// Thread Synchronization
Byte [] RDATA = NULL;
Synchronized (byteslist)
{
If (byteslist. Size () & gt; 0)
RDATA = byteslist. Remove (0 );
}
Return RDATA;
}
}
4. Source Code download
Download model code:
Androidthreaduihandler
-End-
The same address: http://doandroid.info/2012/03/15/android%E4%B8%8B%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%90%8Cui%E4%BA%A4%E4%BA%92%E7%9A%84handler%E6%A1%86%E6%9E%B6%E4%BB%A3%E7%A0%81/
You are welcome to repost and indicate the source.