Android Handler的使用
package com.android.tutor; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; public class HandlerDemo extends Activity { //title为setTitle方法提供变量,这里为了方便我设置成了int型 private int title = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTask(), 1, 5000); } private class MyTask extends TimerTask{ @Override public void run() { setTitle("Welcome to Mr Wei‘s blog " + title); title ++; } } }
package com.android.tutor; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class HandlerDemo extends Activity { //title为setTitle方法提供变量,这里为了方便我设置成了int型 private int title = 0; private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: updateTitle(); break; } }; }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTask(), 1, 5000); } private class MyTask extends TimerTask{ @Override public void run() { Message message = new Message(); message.what = 1; mHandler.sendMessage(message); } } public void updateTitle(){ setTitle("Welcome to Mr Wei‘s blog " + title); title ++; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。