1.onResume()完全执行结束了(没有阻塞)的时候才会 在手机上面显示界面。
2.不能在UI thread执行耗时操作 否则ANR ,handler.post或者postDelay并没有启动新线程,只是不阻塞了而已
3.不能在非UI thread操作 UI 控件,需要使用handler
4.使用Thread+Message+Handler解决上述问题
package handler.activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
static final int DOWNSUCCESS = 1;
static final int DOWNFAIL = 0;
ProgressDialog dialog = null;
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// //执行接收到的通知,更新UI 此时执行的顺序是按照队列进行,即先进先出
super.handleMessage(msg);
switch (msg.what) {
case DOWNSUCCESS:
dialog.dismiss();
Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT)
.show();
break;
}
}
};
// *****************************************
// 主线程Handler负责更新UI,Handler与 Thread通过Message通信
private Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
// 耗时操作
SystemClock.sleep(2000);
Message msg = new Message();
msg.what = DOWNSUCCESS;
MainActivity.this.myHandler.sendMessage(msg);
}
});
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) this.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
myThread.start();
dialog = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
}
});
}
}
==================================================================================
5.HandlerThread Looper MessageQueue三者之间的关系
HandlerThread继承于Thread,所以它本质就是个Thread。与普通Thread的差别就在于,它有个Looper成员变量。
这个Looper其实就是对消息队列以及队列处理逻辑的封装,简单说就是 消息队列+消息循环。
Android中每一个线程都跟着一个Looper,Looper可以帮助线程维护一个消息队列,Looper对象的执行需要初始化Looper.prepare方法,
使用Looper.loop方法启动消息队列管理机制,退出时还要使用Looper.release方法释放资源
关于Looper Handler Message的原理 与 线程局部变量 请参考这篇文章
http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html
===============================================================
6.AsyncTask
package androidthread.acitvity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn = null;
private EditText etaddrss = null;
private EditText etcontent =null;
private ProgressBar pb =null;
private ProgressDialog dialog =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) this.findViewById(R.id.btnsend);
etaddrss = (EditText) this.findViewById(R.id.etaddress);
etcontent = (EditText) this.findViewById(R.id.etcontent);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new MyThreads().execute(etaddrss.getText().toString(),etcontent.getText().toString());
}});
}
//一般作为内部类使用
public class MyThreads extends AsyncTask<String,Integer , String>{
int i=0;
@Override
//这里的参数 与 execute 中的相对应
//这个方法不在UI线程(主线程)不能访问UI组件
protected String doInBackground(String... arg) {
//arg[0]+arg[1];
while(i<=100){
//模拟耗时操作
SystemClock.sleep(500);
this.publishProgress(i);
i+=10;
}
return "发送给"+arg[0]+"内容为"+arg[1]+"邮件已成功发送";
}
@Override //参数reslut 是doInBackground()的返回值
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
dialog.dismiss();
pb.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
Toast.makeText(MainActivity.this, "开始发送邮件", Toast.LENGTH_SHORT).show();
dialog = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
pb =new ProgressBar(MainActivity.this,null,android.R.attr.progressBarStyleHorizontal);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(200,20);
params.setMargins(10, 10, 0, 0);
pb.setLayoutParams(params);
pb.setMax(100);// 设置最大值
pb.setProgress(0);
pb.setVisibility(View.VISIBLE);
MainActivity.this.addContentView(pb, params);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... process) {
Log.i("process",process[0].intValue() +"");
pb.setProgress(process[0].intValue() );
super.onProgressUpdate(process);
}
}
}