场景描述:
每隔一秒刷新一下布局中TextView的显示时间。
布局文件代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.timerefresh.MainActivity" >
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
JAVA代码(利用Timer实现):
package com.example.timerefresh;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public static final String TAG = "MainActivity";
private TextView tvTime;
//时间刷新记时器
private Timer timeTimer;
//时间记时器任务
private TimerTask timeTimerTask;
//消息助手
private Handler messageHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTime = (TextView) findViewById(R.id.tv_time);
messageHandler = new MessageHandler();
initTimeTimer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 初始化时间计时器
*/
public void initTimeTimer() {
timeTimer = new Timer(true);
timeTimerTask = new TimeTimerTask();
if (null != timeTimer) {
timeTimer.schedule(timeTimerTask, 1 * 1000);
}
}
/**
* 取消时间计时器
*/
public void removeTimeTimerTask() {
if (null != timeTimer && null != timeTimerTask) {
timeTimerTask.cancel();
}
}
/**
* 时间刷新任务
*/
class TimeTimerTask extends TimerTask {
@Override
public void run() {
Message message = new Message();
Bundle bundle = new Bundle();// 存放数据
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
String currencyTime = df.format(new Date());
bundle.putString("currencyTime", currencyTime);
message.setData(bundle);
messageHandler.sendMessage(message);
}
}
/**
* 刷新时间
* @author Administrator
*/
class MessageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// 此处可以更新UI
Bundle bundle = msg.getData();
String currencyTime = bundle.getString("currencyTime");
MainActivity.this.tvTime.setText(currencyTime);
MainActivity.this.tvTime.invalidate();
Log.i(TAG, "=============刷新时间===========" + currencyTime);
initTimeTimer();
}
}
}