说明:
发送延时消息:
消息立马发送,但不会立即处理,经过设置的延时时长后才会处理
在android中,只有在UIThread中才能直接更新界面
在android中,长时间的工作(联网)都需要在workerThread中执行
在分线程获得服务器数据后,需要立即到主线程去更新界面显示数据
不实用handler实现异步工作:
private ProgressBar loding_bar;
private TextView textViewp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
loding_bar = (ProgressBar) findViewById(R.id.progressBar1);
textViewp = (TextView) findViewById(R.id.textViewp);
}
/**
* 不实用handler实现异步工作
* @param v
*/
public void getSubmit1(View v){
//1.主线程,显示提示视图(progressdialog/progressBar)
loding_bar.setVisibility(View.VISIBLE);
//2分线程,联网亲求,得到响应数据
new Thread(){
public void run() {
String pathString = "";
try {
final String result = requestToString(pathString);
//3.主线程,显示数据
runOnUiThread(new Runnable() {
@Override
public void run() {
textViewp.setText(result);
loding_bar.setVisibility(View.INVISIBLE);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private String requestToString(String pathString) throws Exception {
URL url = new URL(pathString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
baos.close();
is.close();
String result = baos.toString();
connection.disconnect();
return result;
};
}.start();
}
使用handler实现异步工作:
使用步骤:
-
创建Handler成员变量对象,并重新、写其handlerMessage();
-
在分/主线程创建Message对象
-
使用handler对象发送Message
-
在handleMessage()中处理消息
public class MessageActivity extends Activity implements OnClickListener{
private ProgressBar loding_bar;
private TextView textViewp;
//1.创建handler成员变量对象,重写其handlerMessage()
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==1){
//4.在handlerMessage(0中处理消息
String resultString = (String) msg.obj;
textViewp.setText(resultString);
loding_bar.setVisibility(View.INVISIBLE);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
loding_bar = (ProgressBar) findViewById(R.id.progressBar1);
textViewp = (TextView) findViewById(R.id.textViewp);
}
/**
* 使用handler实现异步工作
*
*/
public void getSubmit2(View v){
//1.主线程,显示提示视图
loding_bar.setVisibility(View.VISIBLE);
//2.分线程,联网请求,得到响应数据
new Thread(){
public void run() {
String path = "";
try {
String result = requestToString(path);
//3.主线程显示数据
//3-1分线程创建Message对象
Message message = Message.obtain();
message.what =1;//标识
message.obj = result;
//3-2使用handler对象发送Message
handler.sendMessage(message);
} catch (Exception e) {
// TODO: handle exception
}
};
}.start();
}
public String requestToString(String pathString) throws Exception {
URL url = new URL(pathString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
baos.close();
is.close();
String result = baos.toString();
connection.disconnect();
return result;
}
}