如何建立一个倒数计时器使用线程的Android? [英] how to create a countdown timer in android using thread?
本文介绍了如何建立一个倒数计时器使用线程的Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个显示2日期之间的差异,但我想是显示我想这code,但它不显示任何内容。在倒计时时间差一个Android的活动
谁能帮我解决这个问题???
firstActivity.java
包com.devleb.expandablelistdemo3;
进口java.io.IOException异常;
进口java.text.ParseException;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.Handler;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.TextView;
进口android.widget.Toast;
公共类FirstActivity延伸活动{
长diffSeconds;
长diffMinutes;
长diffHours;
长diffDays;
私人处理程序处理程序;
私人布尔运行=真;
日D1;
日期D2 = NULL;
SimpleDateFormat的格式;
字符串2037 =二〇一四年六月一十二日23:00:00;
//私人DatabaseHelper dbHelper = NULL;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_first);
getDiffDate();
}
公共无效listTeams(视图v){
意图=新的意图(getBaseContext(),MainActivity.class);
startActivity(在);
}
公共无效listGroups(视图v){
意图=新的意图(getBaseContext(),GroupList.class);
startActivity(在);
}
公共无效DisplaySched(视图v){
意图=新的意图(getBaseContext(),MatchScheduleList.class);
startActivity(在);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.first,菜单);
返回true;
}
公共无效getDiffDate(){
// HH 24小时格式(0-23),天计算转换小时
格式=新的SimpleDateFormat(MM / DD / YYYY HH:MM:SS);
D1 =新的日期();
D2 =无效;
处理程序=新的处理程序();
可运行可运行=新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
而(运行){
尝试{
D2 = format.parse(2037);
//以毫秒为单位
长差异= d2.getTime() - d1.getTime();
diffSeconds =差异/ 1000%60;
diffMinutes =差异/(60 * 1000)%60;
diffHours =差异/(60 * 60 * 1000)%24;
diffDays =差异/(24 * 60 * 60 * 1000);
System.out.print(diffDays +天);
System.out.print(diffHours +小时);
System.out.print(diffMinutes +分钟);
System.out.print(diffSeconds +秒。);
视频下载(1000); //1秒
}赶上(InterruptedException异常E){
e.printStackTrace();
}
赶上(例外五){
e.printStackTrace();
}
handler.post(新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
diffSeconds - = 1;
TextView的txtDate =(TextView中)findViewById(R.id.txtDifDate);
字符串resultDate = diffDays +天+ diffHours +小时
+ diffMinutes +最小+ diffSeconds +S;
txtDate.setText(resultDate);
}
});
}
}
};
}
}
解决方案
有很多方法,主要是要采取通知,如果您有更新UI,你必须UI线程上运行,如果我们使用 CountDownTimer
,这是后做这个
公共类DateCountDownTimer扩展CountDownTimer
{
公共DateCountDownTimer(长的startTime,长间隔)
{
超(startTime时,间隔);
}
@覆盖
公共无效onFinish()
{
txvDate.setText(0分钟0秒);
this.cancel();
}
@覆盖
公共无效onTick(长millisUntilFinished)
{
diffSeconds = millisUntilFinished / 1000%60;
diffMinutes = millisUntilFinished /(60 * 1000)%60;
diffHours = millisUntilFinished /(60 * 60 * 1000)%24;
diffDays = millisUntilFinished /(24 * 60 * 60 * 1000);
字符串resultDate = diffDays +天+ diffHours +小时
+ diffMinutes +最小+ diffSeconds +S;
txtDate.setText(resultDate);
}
}
要启动计时器,使用
长差异= d2.getTime() - d1.getTime();
长间隔= 1000;
DateCountDownTimer定时器=新DateCountDownTimer(差异,间隔);
timer.start(); //启动定时器
有各种其他的方式的AsyncTask
。
请不要接受答案,如果有帮助!
i have an android activity that display the difference between 2 dates but what i want is to display the difference in count down time i tried this code but it do not display anything.
can anyone help me to fix this problem ???
firstActivity.java
package com.devleb.expandablelistdemo3;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
long diffSeconds;
long diffMinutes;
long diffHours;
long diffDays;
private Handler handler;
private boolean Running = true;
Date d1;
Date d2 = null;
SimpleDateFormat format;
String dateStop = "06/12/2014 23:00:00";
// private DatabaseHelper dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
getDiffDate();
}
public void listTeams(View v) {
Intent in = new Intent(getBaseContext(), MainActivity.class);
startActivity(in);
}
public void listGroups(View v) {
Intent in = new Intent(getBaseContext(), GroupList.class);
startActivity(in);
}
public void DisplaySched(View v) {
Intent in = new Intent(getBaseContext(), MatchScheduleList.class);
startActivity(in);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
public void getDiffDate() {
// HH converts hour in 24 hours format (0-23), day calculation
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
d1 = new Date();
d2 = null;
handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(Running){
try{
d2 = format.parse(dateStop);
// in milliseconds
long diff = d2.getTime() - d1.getTime();
diffSeconds = diff / 1000 % 60;
diffMinutes = diff / (60 * 1000) % 60;
diffHours = diff / (60 * 60 * 1000) % 24;
diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
Thread.sleep(1000);//1 second
}catch(InterruptedException e){
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
diffSeconds -= 1;
TextView txtDate = (TextView) findViewById(R.id.txtDifDate);
String resultDate = diffDays + "days " + diffHours + "Hours "
+ diffMinutes + "Min " + diffSeconds + "S";
txtDate.setText(resultDate);
}
});
}
}
};
}
}
解决方案
There are many ways, main thing to take notice is if you have to update the ui, you have to run on ui thread, if we do using CountDownTimer
, this is way doing this
public class DateCountDownTimer extends CountDownTimer
{
public DateCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
txvDate.setText("0 min 0 sec");
this.cancel();
}
@Override
public void onTick(long millisUntilFinished)
{
diffSeconds = millisUntilFinished / 1000 % 60;
diffMinutes = millisUntilFinished / (60 * 1000) % 60;
diffHours = millisUntilFinished / (60 * 60 * 1000) % 24;
diffDays = millisUntilFinished / (24 * 60 * 60 * 1000);
String resultDate = diffDays + "days " + diffHours + "Hours "
+ diffMinutes + "Min " + diffSeconds + "S";
txtDate.setText(resultDate);
}
}
To start the timer, use this
long diff = d2.getTime() - d1.getTime();
long interval=1000;
DateCountDownTimer timer=new DateCountDownTimer(diff,interval);
timer.start();//start the timer
There are various other ways AsyncTask
.
Please Do Accept Answer, If it helps!
这篇关于如何建立一个倒数计时器使用线程的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文