首页 > 代码库 > 线程通信之looper之用法--主线程和工作线程
线程通信之looper之用法--主线程和工作线程
说明:(之前只用handle是因为主线程默认就加上Looper.prepare()和Looper.loop()的。所以主线程可以通过handle收发信息,但是如果在thread里面的话,就是工作线程,工作线程的话,默认是没有加上那两段代码的,所以要手动加上,然后再通过handle.sendMessage()发送信息到工作线程才能取到信息)
主显示布局以及代码:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击下载"
/>
<ProgressBar
android:id="@+id/pbDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="2"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/tvDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0%"/>
</LinearLayout>
java代码:
package com.litsoft.main;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ProgressBar pbDownload;
private TextView tvDownload;
private Handler mmainHanlder,mworkHandler;
private static final int DOWNLOAD_START = 0;
private static final int DOWNLOADING = 1;
private static final int DOWNLOAD_FINISH = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initMainHandler();
createWorkHandler();
setListener();
}
private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.btDownload).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
mworkHandler.sendEmptyMessage(DOWNLOAD_START);
}
});
}
private void createWorkHandler() {
// TODO Auto-generated method stub
new Thread(){
public void run() {
Looper.prepare();//创建Looper对象和工作线程中的MessageQueue
mworkHandler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what==DOWNLOAD_START){
for(int i=0;i<=100;i++){
SystemClock.sleep(2000);
pbDownload.setProgress(i);
Message msg1 = new Message();
msg1.what = DOWNLOADING;
msg1.arg1 = i;
mmainHanlder.sendMessage(msg1);
}
Message msg2 = new Message();
msg2.what = DOWNLOAD_FINISH;
mmainHanlder.sendMessage(msg2);
}
};
};
Looper.loop();//轮询工作线程中的消息队列
};
}.start();
}
private void initMainHandler() {
// TODO Auto-generated method stub
mmainHanlder = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case DOWNLOADING:
tvDownload.setText(msg.arg1+"%");
break;
case DOWNLOAD_FINISH:
Toast.makeText(MainActivity.this, "已经完成下载", 20000);
break;
}
}
};
}
private void initview() {
// TODO Auto-generated method stub
pbDownload = (ProgressBar) findViewById(R.id.pbDownload);
tvDownload = (TextView) findViewById(R.id.tvDownload);
}
}
效果:
本文出自 “追随心的彼岸” 博客,谢绝转载!
线程通信之looper之用法--主线程和工作线程