解释:
当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发, 比如说, 你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作。 如果此时需要一个耗时的操作,例如: 联网读取数据, 或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,,如果你放在主线程中的话,界面会出现假死现象, 如果5秒钟还没有完成的话,会收到Android系统的一个错误提示 "强制关闭".
这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的. 会出现如下异常:
即:只有创建该控件的线程才可以操作该控件
这个时候,Handler就出现了 。由于Handler运行在主线程中(UI线程中), 它与子线程可以通过Message对象来传递数据, 这个时候,Handler就承担着接受子线程传过来的Message对象,(里面包含数据) , 把这些消息放入主线程队列中,配合主线程进行更新UI。
例子如下:
1》布局文件: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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cxc.threadandhandler.MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="手动创建线程并与GUI同步 " />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/show_info_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="显示后台任务情况" />
<Button
android:id="@+id/start_task_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Task" />
</LinearLayout>
</LinearLayout>
效果如下:
2》MainActivity.java代码
代码中有很详细的备注
package com.cxc.handlerdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
运行的结果为:
package com.cxc.threadandhandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String TAG = "com.cxc.threadandhandler.mainactivity";
private static final int MESSAGE_KEY = 1001;
private TextView show_info_tv;
private Button start_task_bt;
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == MESSAGE_KEY) {
// 接收消息并更新UI
Log.d(TAG, "--thread:" + Thread.currentThread().getId()+"--reciver:"+msg.obj.toString());
show_info_tv.setText(msg.obj.toString());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "---main---" + Thread.currentThread().getId());
Log.d(TAG, "---onCreate---");
initViews();
}
private void initViews() {
Log.d(TAG, "---initViews---");
show_info_tv = (TextView) findViewById(R.id.show_info_tv);
start_task_bt = (Button) findViewById(R.id.start_task_bt);
start_task_bt.setOnClickListener(myButtonClickListener);
}
OnClickListener myButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.start_task_bt:
// to-do
// 将耗时的操作移到子线程中
Log.d(TAG, "---Start Task Button Click---");
BackgroundThread backgroundThread = new BackgroundThread();
new Thread(backgroundThread).start();// 启动后台线程,以处理耗时任务
break;
default:
break;
}
}
};
class BackgroundThread implements Runnable {
public BackgroundThread() {
// TODO Auto-generated constructor stub
}
@Override
public void run() {
// TODO Auto-generated method stub
Log.d(TAG, "-----work thread ---" + Thread.currentThread().getId());
backgroundThreadProcessing();
}
// 在后台执行一些处理的方法---耗时操作
private void backgroundThreadProcessing() {
// to-do 耗时操作
Log.d(TAG, "-----work thread -backgroundThreadProcessing ---"
+ Thread.currentThread().getId());
for (int i = 0; i < 10; i++) {
String text = show_info_tv.getText().toString();
text += ("#" + i);
Message msg = new Message();
msg.what = MESSAGE_KEY;//用于区分不同的消息
msg.obj = text;//消息内容
Log.d(TAG, "--thread:" + Thread.currentThread().getId()+"--send:"+text);
myHandler.sendMessage(msg);//发送消息
// 当前线程(后台子线程)休眠1000ms
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
运行结果:
可以看到主线程(UI线程)已经接收到了子线程发送过来的数据。
这一点也可以从程序的Log看出来
06-02 16:25:19.299: D/com.cxc.threadandhandler.mainactivity(10304): ---main---1
06-02 16:25:19.299: D/com.cxc.threadandhandler.mainactivity(10304): ---onCreate---
06-02 16:25:19.299: D/com.cxc.threadandhandler.mainactivity(10304): ---initViews---
06-02 16:25:30.259: D/com.cxc.threadandhandler.mainactivity(10304): ---Start Task Button Click---
06-02 16:25:30.259: D/com.cxc.threadandhandler.mainactivity(10304): -----work thread ---6346
06-02 16:25:30.259: D/com.cxc.threadandhandler.mainactivity(10304): -----work thread -backgroundThreadProcessing ---6346
06-02 16:25:30.259: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0
06-02 16:25:30.259: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0
06-02 16:25:31.259: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1
06-02 16:25:31.259: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1
06-02 16:25:32.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2
06-02 16:25:32.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2
06-02 16:25:33.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3
06-02 16:25:33.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3
06-02 16:25:34.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4
06-02 16:25:34.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4
06-02 16:25:35.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4#5
06-02 16:25:35.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4#5
06-02 16:25:36.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4#5#6
06-02 16:25:36.264: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4#5#6
06-02 16:25:37.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4#5#6#7
06-02 16:25:37.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4#5#6#7
06-02 16:25:38.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4#5#6#7#8
06-02 16:25:38.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4#5#6#7#8
06-02 16:25:39.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:6346--send:显示后台任务情况#0#1#2#3#4#5#6#7#8#9
06-02 16:25:39.269: D/com.cxc.threadandhandler.mainactivity(10304): --thread:1--reciver:显示后台任务情况#0#1#2#3#4#5#6#7#8#9
可以看出,主线程(UI线程)的线程ID为1,而我们自己启动的线程ID为6346,可以说明它们确实不在一个线程中。
关于线程的相关知识可以参考:Java线程之两种方法Runnable和Thread的区别