布局文件就只放了一个简单的ImageView,就不展示了。

下面是Activity

 package com.example.administrator.handlerthreadmessagedemo;

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Timer; public class MainActivity extends Activity { private int images[] = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3}; //存放图片 private ImageView imageView;
private int index = 0; //计算
private TextView textView;
private Timer timer = new Timer();
private Timertask timertask; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
textView = (TextView) findViewById(R.id.title);
timertask = new Timertask(); //放回到Timer中
timer.schedule(timertask,0,1000); //使用Timer 每隔1秒发送一次空消息
} private Handler mhandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 0x111){
index++;
imageView.setBackgroundResource(images[index % 3]);
}
}
}; class Timertask extends java.util.TimerTask{ @Override
public void run() {
mhandler.sendEmptyMessage(0x111);
}
} }
04-14 00:44