android6.0计时器,Android计时器?如何?

好吧,因为这还没有解决,有3种简单的方法来处理这个问题.

下面是一个示例,显示所有3和底部是一个示例,显示我认为更可取的方法.还记得在onPause中清理你的任务,必要时保存状态.

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.os.Handler.Callback;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class main extends Activity {

TextView text, text2, text3;

long starttime = 0;

//this posts a message to the main thread from our timertask

//and updates the textfield

final Handler h = new Handler(new Callback() {

@Override

public boolean handleMessage(Message msg) {

long millis = System.currentTimeMillis() - starttime;

int seconds = (int) (millis / 1000);

int minutes = seconds / 60;

seconds = seconds % 60;

text.setText(String.format("%d:%02d", minutes, seconds));

return false;

}

});

//runs without timer be reposting self

Handler h2 = new Handler();

Runnable run = new Runnable() {

@Override

public void run() {

long millis = System.currentTimeMillis() - starttime;

int seconds = (int) (millis / 1000);

int minutes = seconds / 60;

seconds = seconds % 60;

text3.setText(String.format("%d:%02d", minutes, seconds));

h2.postDelayed(this, 500);

}

};

//tells handler to send a message

class firstTask extends TimerTask {

@Override

public void run() {

h.sendEmptyMessage(0);

}

};

//tells activity to run on ui thread

class secondTask extends TimerTask {

@Override

public void run() {

main.this.runOnUiThread(new Runnable() {

@Override

public void run() {

long millis = System.currentTimeMillis() - starttime;

int seconds = (int) (millis / 1000);

int minutes = seconds / 60;

seconds = seconds % 60;

text2.setText(String.format("%d:%02d", minutes, seconds));

}

});

}

};

Timer timer = new Timer();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

text = (TextView)findViewById(R.id.text);

text2 = (TextView)findViewById(R.id.text2);

text3 = (TextView)findViewById(R.id.text3);

Button b = (Button)findViewById(R.id.button);

b.setText("start");

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Button b = (Button)v;

if(b.getText().equals("stop")){

timer.cancel();

timer.purge();

h2.removeCallbacks(run);

b.setText("start");

}else{

starttime = System.currentTimeMillis();

timer = new Timer();

timer.schedule(new firstTask(), 0,500);

timer.schedule(new secondTask(), 0,500);

h2.postDelayed(run, 0);

b.setText("stop");

}

}

});

}

@Override

public void onPause() {

super.onPause();

timer.cancel();

timer.purge();

h2.removeCallbacks(run);

Button b = (Button)findViewById(R.id.button);

b.setText("start");

}

}

要记住的主要事情是UI只能从主ui线程修改,所以使用处理程序或activity.runOnUIThread(Runnable r);

这是我认为首选的方法.

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class TestActivity extends Activity {

TextView timerTextView;

long startTime = 0;

//runs without a timer by reposting this handler at the end of the runnable

Handler timerHandler = new Handler();

Runnable timerRunnable = new Runnable() {

@Override

public void run() {

long millis = System.currentTimeMillis() - startTime;

int seconds = (int) (millis / 1000);

int minutes = seconds / 60;

seconds = seconds % 60;

timerTextView.setText(String.format("%d:%02d", minutes, seconds));

timerHandler.postDelayed(this, 500);

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test_activity);

timerTextView = (TextView) findViewById(R.id.timerTextView);

Button b = (Button) findViewById(R.id.button);

b.setText("start");

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Button b = (Button) v;

if (b.getText().equals("stop")) {

timerHandler.removeCallbacks(timerRunnable);

b.setText("start");

} else {

startTime = System.currentTimeMillis();

timerHandler.postDelayed(timerRunnable, 0);

b.setText("stop");

}

}

});

}

@Override

public void onPause() {

super.onPause();

timerHandler.removeCallbacks(timerRunnable);

Button b = (Button)findViewById(R.id.button);

b.setText("start");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值