关于handler的线程通信问题
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress=0;
progressBar=(ProgressBar)findViewById(R.id.progressbar);
progressBar.setMax(100);//设置进度条的最大值
new Thread(new Runnable()
{
@Override
public void run() {
Log.e("TAG", "当前的线程是:"+Thread.currentThread().getName());//打印额结果是Thread-28452
// TODO Auto-generated method stub
while(ProgressStatus<=100)
{
ProgressStatus=doSomeWork();
//update the progressbar
handler.post(new Runnable(){
public void run(){
Log.e("TAG", "当前的线程可能是:"+Thread.currentThread().getName());//打印的是main
progressBar.setProgress(ProgressStatus);
}
});
}
//hides the progressbar when the thread gone;
handler.post(new Runnable()
{
public void run()
{
Log.e("TAG", "heh当前的线程可能是:"+Thread.currentThread().getName());//打印的是 main
progressBar.setVisibility(View.GONE);
}
}
);
}
public int doSomeWork()
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ++progress;
}
}).start();
}