这是个android计时的代码,保存着方便以后工作中用到。
public class MainActivity extends Activity {
private TextView tv;
private static long start,now;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvTime);
start = System.currentTimeMillis();
handle.post(runnable);
}
public String showTimeCount(long time) {
if(time >= 360000000){
return "00:00:00";
}
String timeCount = "";
long hourc = time/3600000;
String hour = "0" + hourc;
hour = hour.substring(hour.length()-2, hour.length());
long minuec = (time-hourc*3600000)/(60000);
String minue = "0" + minuec;
minue = minue.substring(minue.length()-2, minue.length());
long secc = (time-hourc*3600000-minuec*60000)/1000;
String sec = "0" + secc;
sec = sec.substring(sec.length()-2, sec.length());
timeCount = hour + ":" + minue + ":" + sec;return timeCount;
}
Handler handle = new Handler();
Runnable runnable = new Runnable() {
@Overridepublic void run() {
handle.postDelayed(this, 500);
now = System.currentTimeMillis();
long time = now - start;
tv.setText(showTimeCount(time));
Log.i("0630","handle -- running");
}
};
}