Android系统规定,一些耗时的操作不能放在UI线程中去执行,这样会报一个ANR错误。所以为了避免该问题,我们需要开启一个新的线程去执行一些耗时操作;开启新的线程,将耗时的操作在新线程里面去执行, 但是子线程中不能更新UI界面,所以我们使用android的Handler机制可以解决这个问题。
详细解释如下:
当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发, 比如说, 你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作。 如果此时需要一个耗时的操作,例如: 联网读取数据, 或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象, 如果5秒钟还没有完成的话,,会收到Android系统的一个错误提示 "强制关闭". 这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的. 这个时候,Handler就出现了.,来解决这个复杂的问题 , 由于Handler运行在主线程中(UI线程中), 它与子线程可以通过Message对象来传递数据, 这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据) , 把这些消息放入主线程队列中,配合主线程进行更新UI。
1。图示说明:
用一个实例来说明:
2.XML代码:
<RelativeLayout 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: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=".MainActivity" > <TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="更新UI界面" /> </RelativeLayout>
3.java代码:
package com.example.handler; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity {
private Mythread mythread;
private TextView mTextView;
private Button mButton; private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
String name = (String) msg.obj;
int m = msg.arg1;
mTextView.append(name + m);
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) this.findViewById(R.id.msg);
mButton = (Button) this.findViewById(R.id.btn);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mythread = new Mythread();
new Thread(mythread).start();
}
});
} private class Mythread implements Runnable {
@Override
public void run() {
int count = ;
while (count <= ) { try {
Thread.sleep();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message message = Message.obtain();// 这里虽然可以new一个消息实例,但是最好别new.
message.arg1 = count;
message.obj = "summer";
handler.sendMessage(message);
count++;
}
}
} @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;
}
}