2
16
package com.example.tianfei.cldevicesdkdemo; import android.os.Handler; import android.os.Message; import android.widget.TextView; import java.util.ArrayList; import java.util.TimerTask; /** * @author LuoSiYe * Created on 2017/4/17. */ public class MyTimerTask extends TimerTask { private ArrayList<String> data; private TextView textView; private int index = 0; public MyTimerTask(TextView text) { init(); textView = text; } private void init() { data = new ArrayList<>(); data.add("0"); data.add("1"); data.add("2"); data.add("3"); data.add("4"); data.add("5"); data.add("6"); data.add("7"); data.add("8"); data.add("9"); } @Override public void run() { if (index == 9) index = 0; Message message = new Message(); myHandler.sendMessage(message); index++; } private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); textView.setText(data.get(index)); } }; }
测试类:
package com.example.tianfei.cldevicesdkdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
/**
* @author LuoSiYe
* Created on 2017/4/17.
*/
public class TestActivity extends Activity {
private TextView testText;
private MyTimerTask myTimerTask;
private Timer mTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
init();
}
private void init() {
testText = (TextView) findViewById(R.id.test_text);
startTimer(testText);
}
private void startTimer(TextView view) {
if (mTimer == null) {
mTimer = new Timer();
myTimerTask = new MyTimerTask(view);
mTimer.schedule(myTimerTask, 0, 1000);
}
}
}
没有测试,你本人测试一下,大致是这样。
2