findViewById() not working when using a seperate class - java

Hi I am new to Android
I would like keep the following code seperatly from my MainActivity file. However, when I try to findViewById() inside the seperate class, I get the error
"cannot be resolved"
Now I know I cannot extend the MainActivity class as it will lead to stack overflow, but could someone tell me how to go about access a textview from this seperate file?
public class Counter {
private TextView tempTextView;
//Temporary TextView
private Button tempBtn;
public Handler mHandler = new Handler();
public long startTime;
public long elapsedTime;
public final int REFRESH_RATE = 100;
public String hours,minutes,seconds,milliseconds;
public long secs,mins,hrs,msecs;
public boolean stopped = false;
public Runnable startTimer = new Runnable() {
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
updateTimer(elapsedTime);
mHandler.postDelayed(this,REFRESH_RATE);
}
};
private void updateTimer (float time) {
secs = (long) (time / 1000);
mins = (long) ((time / 1000) / 60);
hrs = (long) (((time / 1000) / 60) / 60); /* Convert the seconds to String * and format to ensure it has * a leading zero when required */
secs = secs % 60;
seconds = String.valueOf(secs);
if (secs == 0) {
seconds = "00";
}
if (secs < 10 && secs > 0) {
seconds = "0" + seconds;
} /* Convert the minutes to String and format the String */
mins = mins % 60;
minutes = String.valueOf(mins);
if (mins == 0) {
minutes = "00";
}
if (
mins < 10 && mins > 0
) {
minutes = "0" + minutes;
} /* Convert the hours to String and format the String */
hours = String.valueOf(hrs);
if (hrs == 0) {
hours = "00";
}
if (hrs < 10 && hrs > 0) {
hours = "0" + hours;
}
/* Although we are not using milliseconds on the timer in this example * I included the code in the event that you wanted to include it on your own */
milliseconds = String.valueOf((long) time);
if (milliseconds.length() == 2) {
milliseconds = "0" + milliseconds;
}
if (milliseconds.length() <= 1) {
milliseconds = "00";
}
milliseconds = milliseconds.substring(milliseconds.length() - 3, milliseconds.length() - 2);
/* Setting the timer text to the elapsed time */
// ((TextView) findViewById(R.id.elapsed_value)).setText(hours + ":" + minutes + ":" + seconds);
// ((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds); }
}
}

You need to inflate a view and call the mehod findViewById() from this view. To inflate a view, you need a Context (you can set it in a custom constructor)
View view;
LayoutInflater inflater = (LayoutInflater) yourContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
tempTextView = (TextView) view.findViewById(R.id.tv);

Because, findViewById() is method from Activity class. That's why you can not use it in any other Java class.
And second you can not update Android application UI from any worker thread.
So better to use AsyncTask and pass the TextView's references which you want to update after certain task.
Or use runOnMainUiThread() if you are going to update Application UI from other worker thread. But Be sure runOnUiThread() only works with Activity Context.

You could create a constructor for Counter that takes a reference to your TextView from your Activity.
public Counter(TextView tv) {
tempTextView = tv;
}
Then call findViewById() on your MainActivity as pass in the result when you're instantiating Counter.
You're already on the UI thread, no need for AsyncTask here...

You can have a constructor where you set an activity variable.
public Counter (MainActivity activity) {
mActivity = activity;
}
Then, you can call
mActivity.findViewById(...);
which will find the view in your MainActivity layout that you set in
setContentView(<layout>);

Related

Android countDownTimer with Progressbar. Can't setProgress

I am struggling with wierd issue, I've made countdown timer with round progressbar. Sometimes as the time runs out, there is few pixels of progressbar left (orange between x and t), even though it reaches onFinish function. It's not a problem no more as I set countDownInterval to 10, but then showProgress function doesn't quite work, it enters else statement, but doesn't actually set progressbar to 0.
private void startCountingTime() {
if (!counting) {
timeLeftInMillis = maxTime * 1000;
endTime = System.currentTimeMillis() + timeLeftInMillis;
}
if (maxTime != 0) {
countDownTimer = new CountDownTimer(timeLeftInMillis, 100) {
#Override
public void onTick(long l) {
String text = String.format(Locale.getDefault(), "%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(l) % 60,
TimeUnit.MILLISECONDS.toSeconds(l) % 60);
timeLeftInMillis = l;
showProgress(l, maxTime);
counting = true;
question.setText(exposePasswd ? text : currentQuestion);//displays the timer or question
}
#Override
public void onFinish() {
step = 4;
counting = false;
progressBar.setProgress(0);
question.setText("Out of time! doubletap for next question");
}
}.start();
} else {
question.setText("tap to reveal, doubletap for next");
exposePasswd = false;
}
}
private void showProgress(long l, int maxTime) {
maxTime = maxTime * 1000;
int prog = (int) ((l * 100) / maxTime);
if (exposePasswd) {
// progressBar.setVisibility(View.VISIBLE);
if (progressBar.getProgress() == 0) {
progressBar.setProgress(prog);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
progressBar.setProgress(prog, true);
} else {
progressBar.setProgress(prog);
}
} else {
progressBar.setProgress(0);
Log.wtf("idk","setted");
}
}
Any ideas how to fix it?
Im not sure, why this is happening, but you could probably easily bypass this issue by adding:
progressbar.setVisibility(Visibility.GONE);
in the else statement and
progressbar.setVisibility(Visibility.VISIBLE);
when the timer starts.

Display days, hours... that have past since a date in Android (Chronometer)

I have made a Countdown to a future date (with remaining days, hours, minutes, seconds) using CountDownTimer and lots of code from this answer: https://stackoverflow.com/a/32773716/3984944
Now I want to do exactly the same but counting up from a past date. My TextView should refresh every second and show the elapsed time.
What I tried:
I tried manipulating the CountDownTimer so that it works in reverse order. Changing the interval to -1000 or adding 2000 milliseconds to the Countdown every second. Both didn't work.
Then I figured I should use the Chronometer class. The standard Chronometer only displays hours, minutes and seconds as far as I'm concerned. So no days. I then wrote the following code in the style of the CountDownTimer answer I found before that updates a TextView with the desired data:
final Chronometer ch = (Chronometer) findViewById(R.id.ch_chronometer);
final TextView tv = (TextView) findViewById(R.id.tv_show_stopwatch);
ch.setBase(endMillis); //endMillis is the date in Milliseconds
chCountdownSince.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer cArg) {
long t = System.currentTimeMillis() - chCountdownSince.getBase();
long days = TimeUnit.MILLISECONDS.toDays(t);
t -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(t);
t -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(t);
t -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(t);
String stopwatchDisplay = "Days: %d Hours: %d Minutes: %d Seconds: %d";
stopwatchDisplay = String.format(stopwatchDisplay, days, hours, minutes, seconds);
tv.setText(stopwatchDisplay);
}
});
I feel like this snipped makes absolute sense but upon execution it doesn't change my TextView at all. I feel like this is just not how Chronometer works but I don't know what I'm doing wrong.
Edit :
I think you forgot to start Chronometer completely.
Given that
The calls to onTick(long) are synchronized to this object so that one
call to onTick(long) won't ever occur before the previous callback is
complete.
Its unlikely that ticks are done on UI thread, but this is exactly where you need to set your text, try changing
tv.setText(stopwatchDisplay);
to
tv.post(new Runnable() {
public void run() {
tv.setText(stopwatchDisplay);
});
please use handler..
public void countDownStart() {
handler = new Handler();
runnable = new Runnable(){
#Override
public void run(){
handler.postDelayed(this,1000);
try {
FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 3, 01, 2017);
new CountDownTimer(timer.getIntervalMillis(), 1000) {
#Override
public void onTick(long millisUntilFinished){
int days = (int) ((millisUntilFinished / 1000) / 86400);
int hours = (int) (((millisUntilFinished / 1000)
- (days * 86400)) / 3600);
int minutes = (int) (((millisUntilFinished / 1000)
- (days * 86400) - (hours * 3600)) / 60);
int seconds = (int) ((millisUntilFinished / 1000) % 60);
String countdown = String.format("%02dd %02dh %02dm %02ds", days,
hours, minutes, seconds);
txtTimerDay.setText("" + String.format("%02d", days));
txtTimerHour.setText("" + String.format("%02d", hours));
txtTimerMinute.setText(""
+ String.format("%02d", minutes));
txtTimerSecond.setText(""
+ String.format("%02d", seconds));
}
#Override
public void onFinish() {
textViewGone();
MainActivity.aSwitch.setChecked(false);
creditText.setText("Toggle On To Start");
}
}.start();
} catch (Exception e) {
e.printStackTrace(); }
}
};
handler.postDelayed(runnable, 1 * 1000);
}
Remember, 9 is Hours,3 is date,1 is Febraury Month..Month starts from 0th Index
FestCountdownTimer class
public class FestCountdownTimer {
private long intervalMillis;
public FestCountdownTimer(int second, int minute, int hour, int monthDay, int month, int year) {
Time futureTime = new Time();
// Set date to future time
futureTime.set(second, minute, hour, monthDay, month, year);
futureTime.normalize(true);
long futureMillis = futureTime.toMillis(true);
Time timeNow = new Time();
// Set date to current time
timeNow.setToNow();
timeNow.normalize(true);
long nowMillis = timeNow.toMillis(true);
// Subtract current milliseconds time from future milliseconds time to retrieve interval
intervalMillis = futureMillis - nowMillis;
}
public long getIntervalMillis() {
return intervalMillis;
}
}
Hope it helps.. :)

Timer not working properly in ListView

I have used ListView to create my order cards. In which I had run a continuous timer for each card using handler to check how much time has been spent. I have used code in adapter. But my problem is when there is more then one order then the timer time overlaps on each card. For example, if I have 3 cards then ist card will show time of 1,2,3 one by one at interval of one second and 2nd will show time of 2 n 3 and for 3rd timer is stop. Another problem is timer stops after a particular amount if time. Help me regarding this.
This is my piece of code of custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vv = LayoutInflater.from(mContext).inflate(R.layout.adapter_listview_newpage_card,null,false);
menuModels = new ArrayList<>();
// menuModels.clear();
order_time = (TextView) vv.findViewById(R.id.order_time);
// This is my handler of timer:
final Handler myHandler = new Handler(); //declare this line in class
myHandler.postDelayed( // called this line from createlaytout function first time
new Runnable() {
public void run() {
TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
// Log.e("TAG", "timezone "+tz );
Calendar cd = Calendar.getInstance(tz);
int min0 = cd.get(Calendar.MINUTE);
int sec0 = cd.get(Calendar.SECOND);
int hour0 = cd.get(Calendar.HOUR_OF_DAY);
// int ampm0 = cd.get(Calendar.AM_PM);
int total = (hour0 * 60 * 60) + (min0 * 60) + sec0;
int finalTime = total - Integer.parseInt(data.getTimer_time());
int hoursnew = 0;
int seconds = finalTime;
int minutes = seconds / 60;
seconds = seconds % 60;
if (minutes > 59) {
hoursnew = minutes / 60;
minutes = minutes % 60;
}
// Log.e("TAG", "timer" + minutes + "-" + seconds + "$$" + data.getOrder_id());
order_time.setText(" " + data.getOrder_id() + "&" + String.format("%02d", hoursnew) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds));
myHandler.postDelayed(this, 1000);
}
}, 0);
// Here order_time is textview where I add time.
}
Updated code
Hi, I was able to run timer in my code but the problem is that now i am having null pointer exception at adapter.setnotifydata change.
My code for adapter where I set time on textview
public class customadapetr_new extends BaseAdapter {
public customadapetr_new(ArrayList<DataModel> dataModels, Context context, NewPageActivity objNewPageActivity) {
this.dataSet=dataModels;
this.mContext=context;
this.objNewPageActivity = objNewPageActivity;
}
private void setTime(final int order_time, final TextView tv) {
TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
// Log.e("TAG", "timezone "+tz );
Calendar cd = Calendar.getInstance(tz);
int min0 = cd.get(Calendar.MINUTE);
int sec0 = cd.get(Calendar.SECOND);
int hour0 = cd.get(Calendar.HOUR_OF_DAY);
// int ampm0 = cd.get(Calendar.AM_PM);
int total = (hour0 * 60 * 60) + (min0 * 60) + sec0;
int finalTime = total - order_time;
int hoursnew = 0;
int seconds = finalTime;
int minutes = seconds / 60;
seconds = seconds % 60;
if (minutes > 59) {
hoursnew = minutes / 60;
minutes = minutes % 60;
}
tv.setText(" " + String.format("%02d", hoursnew) + ":" + String.format("%02d", minutes));
}
}
And my code from where I have called handler
public class NewPageActivity extends Fragment{
private final Runnable timerRunnable = new Runnable() {
#Override
public void run() {
Log.e("TAG", "run:timer run run " );
adapter.notifyDataSetChanged();
timerHandler.postDelayed(this, 60000); //run every minute
}
};
}
I am getting error at adapter.notifyDataSetChanged() and attaching scrrenshot link of error i.e http://prntscr.com/dmxjr9

Multiple Swing Timers Cause Multiple Actions to Happen

I'm doing a Java project where I have to make a GUI that contains multiple timers that countdown from a user input time. When I create only one timer, the program works fine, but when I try to include multiple timers, each timer will countdown faster based on how many timers there are. I think the problem is being caused by the ActionListener being called multiple times each time the swing timer calls for an action, but I do not know how to solve that problem.
Here is my CountDownTimer class that contains the attributes and methods for the timer:
package pack1;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/*******************************************************************************
* Class that contains the methods needed to create a timer that counts down
* #version January 2014, Project 1
********************************************************************************/
public class CountDownTimer {
/** Amount of hours that are being counted down in the timer */
int hours;
/** Amount of minutes that are being counted down in the timer */
int minutes;
/** Amount of seconds that are being counted down in the timer */
int seconds;
/** Boolean that determines if the add method will work. It is changed with the toggleSuspend method */
static boolean toggle = false;
/*****************************************************************************************
* Default constructor that creates a CountDownTimer object with no time contained in it
*****************************************************************************************/
public CountDownTimer() {
hours = 0;
minutes = 0;
seconds = 0;
}
/*********************************************************************************************
* Constructor that uses the input amount of hours, minutes, and seconds to count down from
*
* Does not allow time that is negative or allow seconds or minutes that are over 60
* #param hours amount of hours that will be counted down from
* #param minutes amount of minutes that will be counted down from
* #param seconds amount of seconds that will be counted down from
*********************************************************************************************/
public CountDownTimer(int hours, int minutes, int seconds) {
super();
// doesn't allow negative minutes, seconds, or hours
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds that are higher than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes that are higher than 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
/*********************************************************************
* Constructor that takes minutes and seconds, and sets hours to zero
* also doesn't allow minutes or seconds to be negative or above 60
* #param minutes amount of minutes that will be counted down from
* #param seconds amount of seconds that will be counted down from
*************************************************************************/
public CountDownTimer(int minutes, int seconds) {
super();
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes to be greater than 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
this.hours = 0;
this.minutes = minutes;
this.seconds = seconds;
}
/***********************************************************************
* Constructor that only takes seconds and sets hours and minutes to 0
* does not allow the seconds to be above 60 or negative
* #param seconds amount of seconds that will be counted down from
*************************************************************************/
public CountDownTimer(int seconds) {
super();
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
this.hours = 0;
this.minutes = 0;
this.seconds = seconds;
}
/**
* Constructor that clones one CountDownTimer object into a new CountDownTimer object
* #param other The CountDownTimer object that is being cloned
* */
public CountDownTimer(CountDownTimer other) {
this.hours = other.hours;
this.minutes = other.minutes;
this.seconds = other.seconds;
}
/*******************************************************************************************************************************
* Constructor that converts a string in the format of "00:00:00" into seconds minutes and hours so it can be counted down from
* #param startTime String that is converted into seconds minutes and hours
*******************************************************************************************************************************/
public CountDownTimer(String startTime) {
// Separates the seconds minutes and hours into an array
String[] parts = startTime.split(":");
// if the array has only one cell, that means only seconds were input
if (parts.length == 1) {
seconds = Integer.parseInt(parts[0]);
}
// if the array has only 2 cells that means there is only minutes and seconds input
if (parts.length == 2) {
minutes = Integer.parseInt(parts[0]);
seconds = Integer.parseInt(parts[1]);
}
// if the array has 3 cells that means there is seconds minutes and hours input
if (parts.length == 3) {
hours = Integer.parseInt(parts[0]);
minutes = Integer.parseInt(parts[1]);
seconds = Integer.parseInt(parts[2]);
}
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than or equal to 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes to be greater than or equal to 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
}
/**************************************************************************************************
* Method that returns true or false based on whether or not two CountDownTimer objects are equal
* #param other Object that is being compared to another CountDownTimer
**************************************************************************************************/
public boolean equals(Object other) {
// converts the two objects to seconds then compares them
if (this.convertToSeconds() == ((CountDownTimer) other)
.convertToSeconds())
return true;
return false;
}
/********************************************************************************
* Returns a boolean based on whether two CountDownTimers, t1 and t2, are equal
* #param t1 first CountDownTimer being compared
* #param t2 second CountDownTimer being compared
********************************************************************************/
public static boolean equals(CountDownTimer t1, CountDownTimer t2) {
// converts the two objects to seconds and then compares them
if (t1.convertToSeconds() == t2.convertToSeconds())
return true;
return false;
}
/************************************************************************************************
* Compares to CountDownTimer objects and returns an int 1, 0, or -1 based on whether the first
* object is greater than, equal to, or less than the CountDownTimer in the parameter
* #param other CountDownTimer that is being compared
*************************************************************************************************/
public int compareTo(CountDownTimer other) {
if (this.convertToSeconds() > other.convertToSeconds())
return 1;
else if (this.convertToSeconds() < other.convertToSeconds())
return -1;
return 0;
}
/**************************************************************************************************
* Compares to CountDownTimer objects and returns an int 1, 0, or -1 based on whether the first
* object (t1) is greater than, equal to, or less than the second object (t2)
* #param t1 first object being compared
* #param t2 second object being compared
* #return
***************************************************************************************************/
public static int compareTo(CountDownTimer t1, CountDownTimer t2) {
if (t1.convertToSeconds() > t2.convertToSeconds())
return 1;
else if (t1.convertToSeconds() < t2.convertToSeconds())
return -1;
return 0;
}
/***************************************************************
* subtracts the input amount of seconds from a CountDownTimer
* #param seconds amount of seconds the user wants to subtract
***************************************************************/
public void subtract(int seconds) {
// places the amount of seconds into an integer
int tempSeconds = this.convertToSeconds();
// subtracts the input seconds from the seconds that were converted
tempSeconds -= seconds;
// converts the new seconds back into the object
formatSeconds(tempSeconds);
}
/*******************************************************************************************
* Subtracts the amount of time contained in one CountDownTimer from another CountDownTimer
* #param other CountDownTimer that is doing the subtracting
*******************************************************************************************/
public void subtract(CountDownTimer other) {
int otherSeconds = other.convertToSeconds();
this.subtract(otherSeconds);
}
/***********************************************************************
* Adds seconds to the object based on what is put into the parameter
* #param seconds amount of seconds being added to the CountDownTimer
************************************************************************/
public void add(int seconds) {
// keeps the method from adding when the toggle is activated
if (toggle == false) {
int tempSeconds = this.convertToSeconds();
tempSeconds += seconds;
formatSeconds(tempSeconds);
} else
throw new IllegalArgumentException(
"Cannot use add when toggle is enabled");
}
/**
* Adds the seconds from one CountDownTimer to another CountDownTimer
* #param other CountDownTimer that is being added to another CountDowntimer
*/
public void add(CountDownTimer other) {
// doesn't allow the method to add when the toggle is true
if (toggle == false) {
int otherSeconds = other.convertToSeconds();
this.add(otherSeconds);
} else
throw new IllegalArgumentException(
"Cannot use add when toggle is enabled");
}
/*******************************************
* Decreases the CountDownTimer by 1 second
*******************************************/
public void dec() {
int tempSeconds = this.convertToSeconds();
tempSeconds--;
formatSeconds(tempSeconds);
}
/****************************************************
* Increases the CountDownTimer object by 1 second
***************************************************/
public void inc() {
int tempSeconds = this.convertToSeconds();
tempSeconds--;
formatSeconds(tempSeconds);
}
/**
* Returns the object as a string in the format of "00:00:00"
*/
public String toString() {
String time = "" + this.hours + ":";
if (this.minutes < 10) {
time += "0" + this.minutes + ":";
} else {
time += this.minutes + ":";
}
if (this.seconds < 10) {
time += "0" + this.seconds;
} else {
time += this.seconds;
}
return time;
}
/************************************************
* Saves the object with a specified name
* #param fileName name of the file being saved
*************************************************/
public void save(String fileName) {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
} catch (IOException e) {
e.printStackTrace();
}
out.println(this.hours);
out.println(this.minutes);
out.println(this.seconds);
out.close();
}
/**************************************************
* Loads the object with the specified name
* #param fileName Name of the file being loaded
**************************************************/
public void load(String fileName) {
try {
Scanner fileReader = new Scanner(new File(fileName));
this.hours = fileReader.nextInt();
this.minutes = fileReader.nextInt();
this.seconds = fileReader.nextInt();
fileReader.close();
System.out.println("Hours: " + this.hours);
System.out.println("Minutes: " + this.minutes);
System.out.println("Seconds: " + this.seconds);
} catch (FileNotFoundException error) {
System.out.println("File not found");
} catch (IOException error) {
System.out.println("OH NO THAT WAS NOT SUPPOSED TO HAPPEN");
}
}
/**********************************************************************************************
* Switches the toggle boolean, and doesn't allow the add methods to work when it is activated
************************************************************************************************/
public static void toggleSuspend() {
if (toggle == false)
toggle = true;
if (toggle == true)
toggle = false;
}
/***********************************************************************************
* Formats a certain amount of seconds and puts it into an existing CountDownTimer
* #param seconds seconds being formatted
***********************************************************************************/
private void formatSeconds(int seconds) {
this.hours = seconds / 3600;
seconds %= 3600;
this.minutes = seconds / 60;
this.seconds = seconds % 60;
}
/*****************************************************************************
* Returns the amount of seconds that are contained in a CountDownTime object
*****************************************************************************/
private int convertToSeconds() {
int hSeconds = hours * 3600;
int mSeconds = minutes * 60;
return hSeconds + mSeconds + seconds;
}
}
And here is my GUI panel class, where I'm having my problem. I'm using arrays to make multiple timers.
package pack1;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class MyTimerPanel extends JPanel {
/** Array of CountDownTimers that will be used */
private CountDownTimer[] cdt;
/** Array of Timer objects that are used to count down by 1 every second*/
private Timer[] javaTimer;
/** Array of TimerListeners that are called in the Timer objects */
private TimerListener timer[];
/** Array of JPanels that will hold each timer interface */
private JPanel[] panel;
/** Array of JLabels for the display of time, seconds, minutes, and hours */
private JLabel[] timeDisplay, secDisplay, minDisplay, hourDisplay;
/** Array of JButtons that start, stop, and set the timer */
private JButton[] start, stop, set;
/** Array of JTextFields that the user will input the seconds, minutes, and hours*/
private JTextField[] sec, min, hour;
/** Array of booleans that will determine if the timer should be counting down */
private boolean[] go;
/** Amount of milliseconds the timer needs to wait before performing an action */
private final int DELAY = 1000;
/** Amount of CountDownTimers in the interfaces */
private final int AMOUNT = 3;
// test integer
int[] count;
/******************************************************************************************
* Constructor that sets the lengths of the arrays and instantiates each part of the array
******************************************************************************************/
public MyTimerPanel() {
count = new int[AMOUNT];
timer = new TimerListener[AMOUNT];
cdt = new CountDownTimer[AMOUNT];
javaTimer = new Timer[AMOUNT];
panel = new JPanel[AMOUNT];
timeDisplay = new JLabel[AMOUNT];
secDisplay = new JLabel[AMOUNT];
minDisplay = new JLabel[AMOUNT];
hourDisplay = new JLabel[AMOUNT];
sec = new JTextField[AMOUNT];
min = new JTextField[AMOUNT];
hour = new JTextField[AMOUNT];
start = new JButton[AMOUNT];
stop = new JButton[AMOUNT];
set = new JButton[AMOUNT];
go = new boolean[AMOUNT];
// Defines each part of each array and adds the neccessary components to the buttons and panels
for (int i = 0; i < AMOUNT; i++) {
count[i] = 0;
cdt[i] = new CountDownTimer(0, 0, 0);
go[i] = false;
timer[i] = new TimerListener();
javaTimer[i] = new Timer(DELAY, timer[i]);
javaTimer[i].start();
timeDisplay[i] = new JLabel(cdt[i].toString());
secDisplay[i] = new JLabel("Seconds: ");
minDisplay[i] = new JLabel("Minutes: ");
hourDisplay[i] = new JLabel("Hours: ");
sec[i] = new JTextField(5);
min[i] = new JTextField(5);
hour[i] = new JTextField(5);
start[i] = new JButton("Start");
stop[i] = new JButton("Stop");
set[i] = new JButton("Set");
start[i].addActionListener(timer[i]);
stop[i].addActionListener(timer[i]);
set[i].addActionListener(timer[i]);
panel[i] = new JPanel();
panel[i].add(hourDisplay[i]);
panel[i].add(hour[i]);
panel[i].add(minDisplay[i]);
panel[i].add(min[i]);
panel[i].add(secDisplay[i]);
panel[i].add(sec[i]);
panel[i].add(start[i]);
panel[i].add(stop[i]);
panel[i].add(set[i]);
panel[i].add(timeDisplay[i]);
add(panel[i]);
}
setPreferredSize(new Dimension(750, 40 * AMOUNT));
}
/*******************************************************************
* Action Listener that activates when certain buttons are pressed
*******************************************************************/
private class TimerListener implements ActionListener {
/********************************************************************
* ActionListener that is activated whenever someone pushes a button
********************************************************************/
public void actionPerformed(ActionEvent e) {
// CountDownTimer that will be compared to at zero
CountDownTimer zero = new CountDownTimer();
// placeholders for the text from the JTextFields
String[] secText = new String[AMOUNT];
String[] minText = new String[AMOUNT];
String[] hourText = new String[AMOUNT];
// runs through each part of the arrays and checks which button needs to be pressed
for (int i = 0; i < AMOUNT; i++) {
// checks if one of the start buttons is being pressed
if (e.getSource() == start[i]){
go[i] = true;
}
// checks if one of the stop buttons is being pressed
else if (e.getSource() == stop[i])
go[i] = false;
// checks if one of the set buttons is being pressed and sets the minutes hours and seconds input
if (e.getSource() == set[i]) {
secText[i] = sec[i].getText();
minText[i] = min[i].getText();
hourText[i] = hour[i].getText();
// if one of the boxes is blank, it will input zero
if (secText[i].equals("")) {
cdt[i].seconds = 0;
} else {
cdt[i].seconds = Integer.parseInt(secText[i]);
}
if (minText[i].equals("")) {
cdt[i].minutes = 0;
} else {
cdt[i].minutes = Integer.parseInt(minText[i]);
}
if (hourText[i].equals("")) {
cdt[i].hours = 0;
} else {
cdt[i].hours = Integer.parseInt(hourText[i]);
}
// sets timeDisplay to show the time in the countdowntimer
timeDisplay[i].setText(cdt[i].toString());
// stops the timer after setting it
go[i] = false;
}
// the timer should be counting down if the go boolean is true or the countdowntimer isn't zero
if (go[i] == true && !cdt[i].equals(zero)) {
javaTimer[i].start();
cdt[i].dec();
timeDisplay[i].setText(cdt[i].toString());
count[i]++;
}
// the timer shouldn't be counting down if the go boolean is false or the countdowntimer is zero
if (go[i] == false || cdt[i].compareTo(zero) <= 0) {
javaTimer[i].stop();
}
}
// for testing purposes
System.out.println(count[0] + "\t" + count[1] + "\t" + count[2]);
}
}
}
The problem is with your TimerListener.
Basically, what's happening, is each time it is called, it executes this code block...
if (go[i] == true && !cdt[i].equals(zero)) {
javaTimer[i].start();
cdt[i].dec();
timeDisplay[i].setText(cdt[i].toString());
count[i]++;
}
This means, if you have 3 counters running, this code block will be executed 9 times per second. Three times per counter/Timer
You TimerListener is too generalised and is trying to do too many things.
Either, create a "single" Timer that ticks on a regular bases and updates each counter as required (so that each loop is dealing with a single counter) OR modify your TimerListener so that it takes details about the counter it is managing and deals ONLY with that counter.
Updated with possible solution
This example uses a single, centralised Timer which simply ticks away in the background. As required, a special Counter is registered with the Timer, which then notifies it when a tick occurs. The Counter then decrements the CountDownTimer and updates the UI.
This is an imperfect example, as I would prefer that the CountDownTimer had a listener interface capable of notifying registered listeners that object's state has changed in some way. This could be achieved through the wrapper Counter class, but I'm to lazy.
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTimerPane {
public static void main(String[] args) {
new TestTimerPane();
}
public TestTimerPane() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
CountDownManager countDownManager = new CountDownManager();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new TimerPane(countDownManager), gbc);
frame.add(new TimerPane(countDownManager), gbc);
frame.add(new TimerPane(countDownManager), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CountDownManager {
private List<Counter> timers;
public CountDownManager() {
timers = new ArrayList<>(25);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Iterator<Counter> it = timers.iterator();
while (it.hasNext()) {
it.next().tick(CountDownManager.this);
}
}
});
timer.start();
}
public void add(Counter timer) {
timers.add(timer);
}
public void remove(Counter timer) {
timers.remove(timer);
}
}
public class TimerPane extends JPanel {
private JTextField fldHours;
private JTextField fldMins;
private JTextField fldSecs;
private JButton btnStart;
private JButton btnStop;
private JButton btnSet;
private JLabel countDown;
private CountDownTimer countDownTimer;
private Counter counter;
public TimerPane(final CountDownManager countDownManager) {
fldHours = new JTextField(2);
fldMins = new JTextField(2);
fldSecs = new JTextField(2);
btnSet = new JButton("Set");
btnStop = new JButton("Stop");
btnStart = new JButton("Start");
countDown = new JLabel("??:??:??");
add(fldHours);
add(new JLabel(":"));
add(fldMins);
add(new JLabel(":"));
add(fldSecs);
add(btnSet);
add(btnStart);
add(btnStop);
add(countDown);
btnSet.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
countDownTimer = new CountDownTimer(
toInt(fldHours),
toInt(fldMins),
toInt(fldSecs));
counter = new Counter(countDown, countDownTimer);
countDown.setText(countDownTimer.toString());
}
});
btnStart.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (counter != null) {
countDownManager.add(counter);
}
}
});
btnStop.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (counter != null) {
countDownManager.remove(counter);
}
}
});
}
protected int toInt(JTextField field) {
int value = 0;
try {
value = Integer.parseInt(field.getText());
} catch (NumberFormatException exp) {
}
return value;
}
}
public class Counter {
private JLabel output;
private CountDownTimer timer;
public Counter(JLabel output, CountDownTimer timer) {
this.output = output;
this.timer = timer;
}
public void tick(CountDownManager manager) {
timer.dec();
output.setText(timer.toString());
if (timer.convertToSeconds() <= 0) {
manager.remove(this);
}
}
}
public static class CountDownTimer {
/**
* Amount of hours that are being counted down in the timer
*/
int hours;
/**
* Amount of minutes that are being counted down in the timer
*/
int minutes;
/**
* Amount of seconds that are being counted down in the timer
*/
int seconds;
/**
* Boolean that determines if the add method will work. It is changed with
* the toggleSuspend method
*/
static boolean toggle = false;
/**
* ***************************************************************************************
* Default constructor that creates a CountDownTimer object with no time
* contained in it
* ***************************************************************************************
*/
public CountDownTimer() {
hours = 0;
minutes = 0;
seconds = 0;
}
/**
* *******************************************************************************************
* Constructor that uses the input amount of hours, minutes, and seconds to
* count down from
*
* Does not allow time that is negative or allow seconds or minutes that are
* over 60
*
* #param hours amount of hours that will be counted down from
* #param minutes amount of minutes that will be counted down from
* #param seconds amount of seconds that will be counted down from
* *******************************************************************************************
*/
public CountDownTimer(int hours, int minutes, int seconds) {
super();
// doesn't allow negative minutes, seconds, or hours
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds that are higher than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes that are higher than 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
/**
* *******************************************************************
* Constructor that takes minutes and seconds, and sets hours to zero also
* doesn't allow minutes or seconds to be negative or above 60
*
* #param minutes amount of minutes that will be counted down from
* #param seconds amount of seconds that will be counted down from
* ***********************************************************************
*/
public CountDownTimer(int minutes, int seconds) {
super();
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes to be greater than 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
this.hours = 0;
this.minutes = minutes;
this.seconds = seconds;
}
/**
* *********************************************************************
* Constructor that only takes seconds and sets hours and minutes to 0 does
* not allow the seconds to be above 60 or negative
*
* #param seconds amount of seconds that will be counted down from
* ***********************************************************************
*/
public CountDownTimer(int seconds) {
super();
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
this.hours = 0;
this.minutes = 0;
this.seconds = seconds;
}
/**
* Constructor that clones one CountDownTimer object into a new
* CountDownTimer object
*
* #param other The CountDownTimer object that is being cloned
*
*/
public CountDownTimer(CountDownTimer other) {
this.hours = other.hours;
this.minutes = other.minutes;
this.seconds = other.seconds;
}
/**
* *****************************************************************************************************************************
* Constructor that converts a string in the format of "00:00:00" into
* seconds minutes and hours so it can be counted down from
*
* #param startTime String that is converted into seconds minutes and hours
* *****************************************************************************************************************************
*/
public CountDownTimer(String startTime) {
// Separates the seconds minutes and hours into an array
String[] parts = startTime.split(":");
// if the array has only one cell, that means only seconds were input
if (parts.length == 1) {
seconds = Integer.parseInt(parts[0]);
}
// if the array has only 2 cells that means there is only minutes and seconds input
if (parts.length == 2) {
minutes = Integer.parseInt(parts[0]);
seconds = Integer.parseInt(parts[1]);
}
// if the array has 3 cells that means there is seconds minutes and hours input
if (parts.length == 3) {
hours = Integer.parseInt(parts[0]);
minutes = Integer.parseInt(parts[1]);
seconds = Integer.parseInt(parts[2]);
}
// doesn't allow seconds minutes or hours to be negative
if (seconds < 0 || minutes < 0 || hours < 0) {
throw new IllegalArgumentException("Time cannot be negative");
}
// doesn't allow seconds to be greater than or equal to 60
if (seconds >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 seconds");
}
// doesn't allow minutes to be greater than or equal to 60
if (minutes >= 60) {
throw new IllegalArgumentException(
"Cannot have more than 60 minutes");
}
}
/**
* ************************************************************************************************
* Method that returns true or false based on whether or not two
* CountDownTimer objects are equal
*
* #param other Object that is being compared to another CountDownTimer
* ************************************************************************************************
*/
public boolean equals(Object other) {
// converts the two objects to seconds then compares them
if (this.convertToSeconds() == ((CountDownTimer) other)
.convertToSeconds()) {
return true;
}
return false;
}
/**
* ******************************************************************************
* Returns a boolean based on whether two CountDownTimers, t1 and t2, are
* equal
*
* #param t1 first CountDownTimer being compared
* #param t2 second CountDownTimer being compared
* ******************************************************************************
*/
public static boolean equals(CountDownTimer t1, CountDownTimer t2) {
// converts the two objects to seconds and then compares them
if (t1.convertToSeconds() == t2.convertToSeconds()) {
return true;
}
return false;
}
/**
* **********************************************************************************************
* Compares to CountDownTimer objects and returns an int 1, 0, or -1 based
* on whether the first object is greater than, equal to, or less than the
* CountDownTimer in the parameter
*
* #param other CountDownTimer that is being compared
* ***********************************************************************************************
*/
public int compareTo(CountDownTimer other) {
if (this.convertToSeconds() > other.convertToSeconds()) {
return 1;
} else if (this.convertToSeconds() < other.convertToSeconds()) {
return -1;
}
return 0;
}
/**
* ************************************************************************************************
* Compares to CountDownTimer objects and returns an int 1, 0, or -1 based
* on whether the first object (t1) is greater than, equal to, or less than
* the second object (t2)
*
* #param t1 first object being compared
* #param t2 second object being compared
* #return
* *************************************************************************************************
*/
public static int compareTo(CountDownTimer t1, CountDownTimer t2) {
if (t1.convertToSeconds() > t2.convertToSeconds()) {
return 1;
} else if (t1.convertToSeconds() < t2.convertToSeconds()) {
return -1;
}
return 0;
}
/**
* *************************************************************
* subtracts the input amount of seconds from a CountDownTimer
*
* #param seconds amount of seconds the user wants to subtract
* *************************************************************
*/
public void subtract(int seconds) {
// places the amount of seconds into an integer
int tempSeconds = this.convertToSeconds();
// subtracts the input seconds from the seconds that were converted
tempSeconds -= seconds;
// converts the new seconds back into the object
formatSeconds(tempSeconds);
}
/**
* *****************************************************************************************
* Subtracts the amount of time contained in one CountDownTimer from another
* CountDownTimer
*
* #param other CountDownTimer that is doing the subtracting
* *****************************************************************************************
*/
public void subtract(CountDownTimer other) {
int otherSeconds = other.convertToSeconds();
this.subtract(otherSeconds);
}
/**
* *********************************************************************
* Adds seconds to the object based on what is put into the parameter
*
* #param seconds amount of seconds being added to the CountDownTimer
* **********************************************************************
*/
public void add(int seconds) {
// keeps the method from adding when the toggle is activated
if (toggle == false) {
int tempSeconds = this.convertToSeconds();
tempSeconds += seconds;
formatSeconds(tempSeconds);
} else {
throw new IllegalArgumentException(
"Cannot use add when toggle is enabled");
}
}
/**
* Adds the seconds from one CountDownTimer to another CountDownTimer
*
* #param other CountDownTimer that is being added to another CountDowntimer
*/
public void add(CountDownTimer other) {
// doesn't allow the method to add when the toggle is true
if (toggle == false) {
int otherSeconds = other.convertToSeconds();
this.add(otherSeconds);
} else {
throw new IllegalArgumentException(
"Cannot use add when toggle is enabled");
}
}
/**
* *****************************************
* Decreases the CountDownTimer by 1 second
* *****************************************
*/
public void dec() {
int tempSeconds = this.convertToSeconds();
tempSeconds--;
formatSeconds(tempSeconds);
}
/**
* **************************************************
* Increases the CountDownTimer object by 1 second
* *************************************************
*/
public void inc() {
int tempSeconds = this.convertToSeconds();
tempSeconds--;
formatSeconds(tempSeconds);
}
/**
* Returns the object as a string in the format of "00:00:00"
*/
public String toString() {
String time = "" + this.hours + ":";
if (this.minutes < 10) {
time += "0" + this.minutes + ":";
} else {
time += this.minutes + ":";
}
if (this.seconds < 10) {
time += "0" + this.seconds;
} else {
time += this.seconds;
}
return time;
}
/**
* **********************************************
* Saves the object with a specified name
*
* #param fileName name of the file being saved
* ***********************************************
*/
public void save(String fileName) {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
} catch (IOException e) {
e.printStackTrace();
}
out.println(this.hours);
out.println(this.minutes);
out.println(this.seconds);
out.close();
}
/**
* ************************************************
* Loads the object with the specified name
*
* #param fileName Name of the file being loaded
* ************************************************
*/
public void load(String fileName) {
try {
Scanner fileReader = new Scanner(new File(fileName));
this.hours = fileReader.nextInt();
this.minutes = fileReader.nextInt();
this.seconds = fileReader.nextInt();
fileReader.close();
System.out.println("Hours: " + this.hours);
System.out.println("Minutes: " + this.minutes);
System.out.println("Seconds: " + this.seconds);
} catch (FileNotFoundException error) {
System.out.println("File not found");
} catch (IOException error) {
System.out.println("OH NO THAT WAS NOT SUPPOSED TO HAPPEN");
}
}
/**
* ********************************************************************************************
* Switches the toggle boolean, and doesn't allow the add methods to work
* when it is activated
* **********************************************************************************************
*/
public static void toggleSuspend() {
if (toggle == false) {
toggle = true;
}
if (toggle == true) {
toggle = false;
}
}
/**
* *********************************************************************************
* Formats a certain amount of seconds and puts it into an existing
* CountDownTimer
*
* #param seconds seconds being formatted
* *********************************************************************************
*/
private void formatSeconds(int seconds) {
this.hours = seconds / 3600;
seconds %= 3600;
this.minutes = seconds / 60;
this.seconds = seconds % 60;
}
/**
* ***************************************************************************
* Returns the amount of seconds that are contained in a CountDownTime
* object
* ***************************************************************************
*/
private int convertToSeconds() {
int hSeconds = hours * 3600;
int mSeconds = minutes * 60;
return hSeconds + mSeconds + seconds;
}
}
}
Now, if you need a separate Timer per CountDownTimer, then I would probably create some kind of "worker" class that would take a CountDownTimer and that had a self contained Timer. I would also provide some kind of listener that interested parties could register to and would allow them to update the UI as they see fit...

TimerTask not executing?

Here is my timer class, This class is designed to constantly update a timer in a view. However, when I run the app the first toast message is displayed to the screen but the second one is never reached (the timerTask's "run" method is never executed). I know that this is probably something simple that I am doing wrong. If anyone could steer me in the right direcion that would be great.
public class MyTimer {
static Timer _timerTask = new Timer();
static int totalSeconds = 1, hour = 0, min = 0, sec = 0;
static String mTimeFormat = "%02d:%02d:%02d";
static String timeTakenString;
public static void start (){
Toast.makeText(GPSMain.context, "Message one", Toast.LENGTH_LONG).show();
TimerTask timer = new TimerTask() {
#Override
public void run() {
Toast.makeText(GPSMain.context, "Message two", Toast.LENGTH_LONG).show();
totalSeconds += 1;
sec += 1;
if(sec >= 60) {
sec = 0;
min += 1;
if (min >= 60) {
min = 0;
hour += 1;
}
}
timeTakenString = String.format(mTimeFormat, hour, min, sec);
postExecute.sendEmptyMessage(0); //update UI
}
private Handler postExecute = new Handler(){
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
GPSMain.timer.setText("Time Taken: "+timeTakenString);
}
};
};
_timerTask.scheduleAtFixedRate(timer,1000,1000);
}
}
code in another file calling this class:
MyTimer myTimer = new MyTimer();
....
myTimer.start();
PROJECT SPEC CHANGED!
My project leader changed the spec of the project so that it no longer needs to update the timer to the UI but rather display it as an end result. Accepting the first answer anyway as it solves the original problem. Will post the new code below.
New code calls:
System.currentTimeMillis();
at the beggining and end of the runcycle, which returns a long. The first value is then subtracted from the second value to calculate the amount of time taken to execute the runcycle. That value is then manipulated and put into a timer format that is displayed at the end as a string.
public static String getTimeTaken(long end, long start){
#SuppressWarnings("unused")
String formattedTime = "", hourHour = "", hourMin = ":", minSec = ":";
long timeTaken = (end-start)/1000, hour = 0, min = 0, sec = 0;
if (timeTaken>9 ){
hourHour = "0";
hourMin = ":0";
if (timeTaken>=60){
if (timeTaken>= 3200){
hour = timeTaken/3200;
timeTaken = timeTaken%3200;
if (hour>9){
hourHour = "";
}
}
min = timeTaken/60;
timeTaken = timeTaken%60;
if (min >9){
hourMin = ":";
}
}
sec = timeTaken;
if(sec%60<10){
minSec = ":0";
}
return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}
sec = timeTaken;
minSec = ":0";
hourMin = ":0";
hourHour = "0";
return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}
Using thread you cant update your UI for that you have to use runOnUiThread
youractivity.this.runOnUiThread(new Runnable(){public void run(){Toast.makeText(mContext, "Message", Toast.LENGTH_LONG).show();}});
(Very late...just answering in case someone reach this question... scheduling a task doesn't garantee it will run on the proper time... it may take longer, sometimes much longer...)

Categories