AndroidManifest.xml
package com.cctvjiatao.timerdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @作者: jiatao
* @修改时间:2016-3-19 下午8:05:47
* @包名:com.cctvjiatao.timerdemo
* @文件名:MainActivity.java
* @版权声明:www.cctvjiatao.com
* @功能: Android三种实现定时器的方法
*/
public class MainActivity extends Activity implements OnClickListener{
private Context mContext;
private Button btn_timer1, btn_timer2, btn_timer3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mContext = this;
initView();
}
private void initView() {
btn_timer1 = (Button) findViewById(R.id.btn_timer1);
btn_timer2 = (Button) findViewById(R.id.btn_timer2);
btn_timer3 = (Button) findViewById(R.id.btn_timer3);
btn_timer1.setOnClickListener(this);
btn_timer2.setOnClickListener(this);
btn_timer3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_timer1:
startActivity(new Intent(mContext,Timer1Activity.class));
break;
case R.id.btn_timer2:
startActivity(new Intent(mContext,Timer2Activity.class));
break;
case R.id.btn_timer3:
startActivity(new Intent(mContext,Timer3Activity.class));
break;
}
}
}
package com.cctvjiatao.timerdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
/**
* @作者: jiatao
* @修改时间:2016-3-19 下午8:17:20
* @包名:com.cctvjiatao.timerdemo
* @文件名:Timer1Activity.java
* @版权声明:www.cctvjiatao.com
* @功能: 运用 Handler+Thread 实现定时器+字幕滚动
*/
public class Timer1Activity extends Activity {
private static String TAG = "Timer1Activity";
private TextView tv_run_h;
private int i = 0;
private int num = 0;
private ArrayList newsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
initData();
tv_run_h = (TextView) findViewById(R.id.tv_run_h);
new Thread(new ShowTextThread()).start();
}
private void initData() {
newsList = new ArrayList<>();
newsList.add("这样创建并启动一个定时器的工作就完成,但是在实际使用的过程中,可能会需要一些其他方面的操作手段。");
newsList.add("因为在项目程序运行过程中,某个时刻定时器的参数被修改,比如delay,period。为了及时的使用最新的正确的数据,就需要对已经存在的定时器进行关闭并且使用新的参数重新创建并启动");
newsList.add("取消TimerTask");
num = newsList.size();
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (newsList != null && newsList.size() > 0) {
tv_run_h.setText((i + 1) + "." + newsList.get(i));
Log.e(TAG, "received");
i++;
}
if (i == num) {
i = 0;
}
}
};
};
class ShowTextThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
Log.e(TAG, "sending....");
} catch (InterruptedException e) {
e.printStackTrace();
Log.e(TAG, "send error");
}
}
}
}
}
package com.cctvjiatao.timerdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
/**
* @作者: jiatao
* @修改时间:2016-3-19 下午8:18:32
* @包名:com.cctvjiatao.timerdemo
* @文件名:Timer2Activity.java
* @版权声明:www.cctvjiatao.com
* @功能: 运用 Handler类自带的postDelyed 实现定时器+字幕滚动
*/
public class Timer2Activity extends Activity {
private static String TAG = "Timer2Activity";
private TextView tv_run_h;
private int i = 0;
private int num = 0;
private ArrayList newsList;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
initData();
tv_run_h = (TextView) findViewById(R.id.tv_run_h);
handler.postDelayed(runnable, 10000);
}
private void initData() {
newsList = new ArrayList<>();
newsList.add("这样创建并启动一个定时器的工作就完成,但是在实际使用的过程中,可能会需要一些其他方面的操作手段。");
newsList.add("因为在项目程序运行过程中,某个时刻定时器的参数被修改,比如delay,period。为了及时的使用最新的正确的数据,就需要对已经存在的定时器进行关闭并且使用新的参数重新创建并启动");
newsList.add("取消TimerTask");
num = newsList.size();
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (newsList != null && newsList.size() > 0) {
tv_run_h.setText((i + 1) + "." + newsList.get(i));
Log.e(TAG, "received");
i++;
}
if (i == num) {
i = 0;
}
}
};
}
package com.cctvjiatao.timerdemo;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
/**
* @作者: jiatao
* @修改时间:2016-3-19 下午8:18:45
* @包名:com.cctvjiatao.timerdemo
* @文件名:Timer3Activity.java
* @版权声明:www.cctvjiatao.com
* @功能: 运用 Handler+Timer+TimerTask 实现定时器+字幕滚动
*/
public class Timer3Activity extends Activity {
private static String TAG = "Timer1Activity";
private TextView tv_run_h;
private int i = 0;
private int num = 0;
private ArrayList newsList;
private Timer timer = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
initData();
tv_run_h = (TextView) findViewById(R.id.tv_run_h);
timer.schedule(task, 1000, 10000);// 1s后执行task,经过10s再次执行
}
private void initData() {
newsList = new ArrayList<>();
newsList.add("这样创建并启动一个定时器的工作就完成,但是在实际使用的过程中,可能会需要一些其他方面的操作手段。");
newsList.add("因为在项目程序运行过程中,某个时刻定时器的参数被修改,比如delay,period。为了及时的使用最新的正确的数据,就需要对已经存在的定时器进行关闭并且使用新的参数重新创建并启动");
newsList.add("取消TimerTask");
num = newsList.size();
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (newsList != null && newsList.size() > 0) {
tv_run_h.setText((i + 1) + "." + newsList.get(i));
Log.e(TAG, "received");
i++;
}
if (i == num) {
i = 0;
}
}
};
};
TimerTask task = new TimerTask() {
@Override
public void run() {
// 需要做的事:发送消息
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}
};
}