码迷,mamicode.com
首页 > 移动开发 > 详细

android handler用法

时间:2015-04-09 20:01:44      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:android   handler   



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);
 }

}

android handler用法

标签:android   handler   

原文地址:http://blog.csdn.net/aimingkun/article/details/44962921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!