Reputation: 95
I have used timer task to run every second.
Used diffSeconds = diff / (1000); to update every 1 second but it is executing only after 60 seconds.
I need to update the timer every second and update the ui with the seconds value for every seconds.
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
/*int n = 0;
if (n ==0){*/
String endTime = DataHandler.getInstance().getEndTime();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+5:30"));
Date currentLocalTime = cal.getTime();
DateFormat df = new SimpleDateFormat("HH:mm");
df.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
String localTime = df.format(currentLocalTime);
Date currentTime = null;
Date dateEndTime = null;
long diff, diffMinutes, diffSeconds;
try {
dateEndTime = df.parse(endTime);
currentTime = df.parse(localTime);
diff = dateEndTime.getTime() - currentTime.getTime();
diffMinutes = diff / (60 * 1000);
diffSeconds = diff / (1000);
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
difference = Long.toString(diffMinutes);
differenceseconds = Long.toString(diffSeconds);
timenumber.setText(differenceseconds);
if(diffSeconds==1){
startAlarm();
//String endTime = DataHandler.getInstance().getEndTime();
}
SupremeLogger.d("localtime", localTime);
SupremeLogger.d("endtime", endTime);
SupremeLogger.d("difference in minutes", difference);
SupremeLogger.d("Difference in seconds", differenceseconds);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}};
t.schedule(mTimerTask, 0, 1000);
}
Upvotes: 1
Views: 143
Reputation: 2293
I did this using an AsyncTask. Here is my code. This will update the TextView
you pass into its constructor every second, displaying it like (minutes):(seconds).
private class TimerUpdateTask extends AsyncTask<Void, Void, Void>{
private static final String TAG = "TimerUpdateTask";
private long startTime;
boolean running = true;
TextView tvTime;
public void kill(){
running = false;
}
public TimerUpdateTask(TextView tvTime){
this.tvTime = tvTime;
}
public void onPreExecute(){
this.startTime = System.currentTimeMillis();
}
@Override
public void onProgressUpdate(Void... p){
long current = System.currentTimeMillis();
long elapsed = current - startTime;
String time = "" + elapsed / 60 + ":"; // does minutes : seconds
if (s % 60 < 10) {
time += "0" + (s % 60);
} else {
time += (s % 60);
}
tvTime.setText(time);
}
@Override
protected Void doInBackground(Void... arg0) {
long last = startTime;
while (running){
while (last / 1000 + startTime % 1000 ==
System.currentTimeMillis() / 1000 + startTime % 1000){
try { // busy waiting with 50 ms rests.
Thread.sleep(50);
} catch (Exception e){}
if (!running) break;
}
last = System.currentTimeMillis();
publishProgress();
}
return null;
}
@Override
public void onPostExecute(Void result){
if (running){
Log.wtf(LAG, "wtf");
} else {
Log.d(TAG, "killed");
}
}
}
Instantiate this TimerUpdateTask
and save it somewhere as a local variable, then, when you want to stop, call its someTimerUpdateTask.kill()
.
Upvotes: 2