package com.example.handler;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HandlerActivity extends Activity {
private Button startbutton = null;
private Button endbutton = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局文件
setContentView(R.layout.handler_activity);
//设置按钮id
startbutton = (Button)findViewById(R.id.start);
endbutton = (Button)findViewById(R.id.end);
//设置按钮文字
startbutton.setText(R.string.start);
endbutton.setText(R.string.end);
//设置按钮监听器
startbutton.setOnClickListener(new StartListener());
endbutton.setOnClickListener(new EndListener());
}
//定义Handler对象
Handler handler = new Handler();
//当点击startbutton按钮之后会执行下面操作
class StartListener implements OnClickListener {
public void onClick(View arg0) {
//使用Handler的post方法把updataThread这个线程加入到队列中,在消息队列中自动执行这个线程
handler.post(updateThread);
}
}
//当点击endbutton按钮之后会执行下面操作
class EndListener implements OnClickListener {
public void onClick(View arg0) {
//从队列中取出线程
handler.removeCallbacks(updateThread);
}
}
//使用Runnable对象实现线程
Runnable updateThread = new Runnable() {
public void run() {
System.out.println("updateThread");
//当updataThread线程开始后会执行run方法,使用Handler的postDelayed方法在把当前线程对象updataThread隔2000毫秒之后加入到队列当中,并开始执行这个线程对象,执行之后又会调用Handler的postDelayed方法隔2000毫秒之后把线程对象updataThread加入到队列当中,依次循环执行这个线程对象...
handler.postDelayed(updateThread, 2000);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
全部代码
package com.example.handler;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HandlerActivity extends Activity {
private Button startbutton = null;
private Button endbutton = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handler_activity);
startbutton = (Button)findViewById(R.id.start);
endbutton = (Button)findViewById(R.id.end);
startbutton.setText(R.string.start);
endbutton.setText(R.string.end);
startbutton.setOnClickListener(new StartListener());
endbutton.setOnClickListener(new EndListener());
}
class StartListener implements OnClickListener {
public void onClick(View arg0) {
handler.post(updateThread);
}
}
class EndListener implements OnClickListener {
public void onClick(View arg0) {
handler.removeCallbacks(updateThread);
}
}
Handler handler = new Handler();
Runnable updateThread = new Runnable() {
public void run() {
System.out.println("updateThread");
handler.postDelayed(updateThread, 2000);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>