handler类的主要作用有两个
(1)在新启动的线程中发送消息
(2)在主线程中获取、处理消息
public class MainActivity extends Activity {
private ImageView iv;
int[] imageids = new int[] { R.drawable.fy_d_go, R.drawable.fy_d_kf,
R.drawable.fy_d_kf2, R.drawable.ic_launcher };
int currentId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
final Handler mhandle = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x1233) {
iv.setImageResource(imageids[currentId++ % imageids.length]);
}
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mhandle.sendEmptyMessage(0x1233);
}
}, 0, 1200);
}
}
时间: 2024-11-07 00:00:06