Android : How to Stop The Text View Updates when button is clicked?

on Tuesday, March 31, 2015


I am new to Android. I have four states like: driving, onDuty, offDuty and sleep. When I click on the driving button Automatically Time Updates in the Textview. It's working fine for me. Here is my code:



btnDriving.setonClickListener(new View.onClickListener)
{
private void UpdatingDrvingCircle() {
new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);

if(getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateDrivingTextView();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}

private void updateDutyTextView(){

final DateTime dt=DateTime.now() ;
String pattern = " KK:mm:ss";
DateTimeFormatter fmt;
fmt=DateTimeFormat.forPattern(pattern);
DateTime et=DateTime.now();
fmt=DateTimeFormat.forPattern(pattern);
String sTime=dt.toString(fmt);
String eTime=et.toString(fmt);

DateTime d1 = fmt.parseDateTime(sTime);
DateTime d2 = fmt.parseDateTime(eTime);

DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
int hours=Hours.hoursBetween(dt1, dt2).getHours() % 24;
int minutes=Minutes.minutesBetween(dt1, dt2).getMinutes() % 60;
int LeftHours=AppConstants.DRIVING_11_HOUR-hours;
int LeftMins=AppConstants.DRIVING_MINUTE-minutes;
tvDutyTimeLeft.setText(LeftHours+" hr "+LeftMins+" m left");

}


Here I have one More Button i.e..btnOnDuty. When I click on that Button the Previous Button Text View Updates are stopped. Could anyone help me figure out why?


Here is btnDuty Code



btnOnDuty.setonClickListener()
{
UpdatingDutyCircle();
}
private void UpdatingDutyCircle() {
Thread t=new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateDutyTextView();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}


}
};t.start();
}

private void updateDutyTextView() {

fmt=DateTimeFormat.forPattern(pattern);
DateTime et=DateTime.now();
fmt=DateTimeFormat.forPattern(pattern);
String sTime=dt.toString(fmt);
String eTime=et.toString(fmt);

DateTime d1 = fmt.parseDateTime(sTime);
DateTime d2 = fmt.parseDateTime(eTime);

DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
int hours=Hours.hoursBetween(dt1, dt2).getHours() % 24;
int minutes=Minutes.minutesBetween(dt1, dt2).getMinutes() % 60;
int LeftHours=AppConstants.DRIVING_14_HOUR-hours;
int LeftMins=AppConstants.DRIVING_MINUTE-minutes;

int TotalHrs=hours*60+minutes;
btnOnDuty.setText(""+hours+"hr "+ minutes +"m");
pbOnDuty.setMax(660);
pbOnDuty.setProgress(TotalHrs);
tvDutyTimeLeft.setText(LeftHours+" hr "+LeftMins+" m left");
}

0 comments:

Post a Comment