Countdown timer problems Android - java

My app checks if there is a file with a date.
If there is a date, it calculates the difference between today and that (future) date and initializes a timer counting down the seconds until that date in the way X days Y hours Z minutes S seconds.
If there is no file, then the user can select a date with a button. The program will store the date in the file and set the countdown.
There is a Delete Button to delete the date and choose another. This delete button should cancel the timer so it stops counting.
The timer does not stop when I cancel it. My timer is ticking but the difference is 2 seconds instead of one. It shows 40 38 36... instead of 40 39 38...
And lastly, it's storing the picked date twice instead of once.
The DatePicker code is from here.
public class NextVisit extends Activity implements DatePickerFragment.TheListener{
protected Vibrator vibrate;
protected int SECONDS_IN_A_DAY = 24 * 60 * 60;
protected String filePath = "";
protected SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
final long[] pattern = {0, 1000, 500, 1000, 500, 1000, 500, 1000 };
CountDownTimer timer = null;
String dateString = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next_visit);
final TextView label = (TextView) findViewById(R.id.label);
final TextView countdown = (TextView) findViewById(R.id.time_visit);
final Button bDate = (Button) findViewById(R.id.date_button);
final Button bDelete = (Button) findViewById(R.id.delete_button);
File dir = new File(getFilesDir(), "loveApp");
if(!dir.exists())
dir.mkdirs();
final File f = new File(getFilesDir()+"/loveApp"+"/love_date.txt");
filePath = f.getAbsolutePath();
vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE);
bDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(f.exists()){
f.delete();
System.out.println("File deleted successfully");
bDate.setVisibility(View.VISIBLE);
label.setText(R.string.next_visit1);
timer.cancel();
countdown.setText("");
System.out.println("I cancelled the timer");
bDelete.setVisibility(View.INVISIBLE);
bDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
bDelete.setVisibility(View.VISIBLE);
bDate.setVisibility(View.INVISIBLE);
picker.show(getFragmentManager(), "datePicker");
}
});
}
}
});
if(f.exists()){
System.out.println("File exists");
bDate.setVisibility(View.INVISIBLE);
label.setText(getString(R.string.label));
try{
BufferedReader br = new BufferedReader(new FileReader(f));
dateString = br.readLine();
br.close();
System.out.println("Date: "+dateString);
Date finalDate = formatter.parse(dateString);
setCountdown(finalDate);
}catch(Exception e){
e.printStackTrace();
label.setText("ERROR");
}
}else{
System.out.println("File DOES NOT EXIST");
bDelete.setVisibility(View.INVISIBLE); //Removing the delete date button
bDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
bDelete.setVisibility(View.VISIBLE);
bDate.setVisibility(View.INVISIBLE);
picker.show(getFragmentManager(), "datePicker");
}
});
}
}
public void setCountdown(Date finishDate){
long end = finishDate.getTime();
final TextView countdown = (TextView) findViewById(R.id.time_visit);
timer = new CountDownTimer(end, 1000){
#Override
public void onTick(long millisUntilFinished) {
long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000;
long diff = millisUntilFinished - now;
long diffSec = diff / 1000;
long days = diffSec / SECONDS_IN_A_DAY;
long secondsDay = diffSec % SECONDS_IN_A_DAY;
long seconds = secondsDay % 60;
long minutes = (secondsDay / 60) % 60;
long hours = (secondsDay / 3600);
countdown.setText(days+ " days, "+ hours + "hours, "+minutes + "m, "+seconds + "s remaining!");
}
#Override
public void onFinish() {
countdown.setText("done!");
vibrate.vibrate(pattern,-1);
}
}.start();
}
#Override
public void returnDate(String date) {
dateString = date;
TextView label = (TextView) findViewById(R.id.label);
label.setText(R.string.label);
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(new File(filePath)));
bw.write(dateString);
bw.close();
System.out.println("Saved the file, date is: "+ dateString);
Date finishDate = formatter.parse(date);
setCountdown(finishDate);
} catch (Exception e) {
e.printStackTrace();
}
}
}

I think problem in this string long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000; you need put it outside of countdowntimer code.
final long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000;
timer = new CountDownTimer(end, 1000){
#Override
public void onTick(long millisUntilFinished) {
long diff = millisUntilFinished - now;
long diffSec = diff / 1000;
...
};

Related

inserting Start, Stop and differenece time into sql database an android studio java language

I have wrote a code when I click on button start it show current time and turn text into stop then I click on stop it show me stop current time and calculate difference. after that I made a class to insert all these three start stop and difference into SQL database.
my problem is when I click on stop button it show me toast time added but value not added into database it show blank column into database. my database table column shows no values.
public class cleaning extends Fragment {
TextView tvstarttime;
Button btn_start;
Button btn_end;
TextView tvend;
TextView diffence;
boolean isStarted = false;
String starttime, endtime, timedifference;
ProgressDialog progressDialog;
Connection conn;
boolean isSucess = false;
NetworkManager networkManager = new NetworkManager();
SharedPrefManager sharedPrefManager = new SharedPrefManager();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_cleaning, container, false);
tvstarttime = view.findViewById(R.id.txtstarttime);
btn_start = view.findViewById(R.id.btn_start);
tvend = view.findViewById(R.id.txtendtime);
uploadcart uploadcart = new uploadcart();
uploadcart.execute();
diffence=view.findViewById(R.id.txtdifference);
starttime = tvstarttime.getText().toString().trim();
endtime = tvend.getText().toString().trim();
timedifference = diffence.getText().toString().trim();
btn_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isStarted){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
String time = format.format(calendar.getTime());
tvend.setText(time);
try {
Date date1 = format.parse(tvend.getText().toString());
Date date2 = format.parse(tvstarttime.getText().toString());
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills % (1000*60*60));
String diff = hours + ":" + mins;
diffence.setText(diff);
} catch (ParseException e) {
e.printStackTrace();
}
btn_start.setText("START");
}else {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
String time1 = format.format(calendar.getTime());
tvstarttime.setText(time1);
btn_start.setText("STOP");
}
isStarted = !isStarted;
}
});
return view;
}
private class uploadcart extends AsyncTask<Void, Void, Void> {
/* #Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(ProductDetailFragment.this);
progressDialog.setTitle("Creating account please wait!");
progressDialog.show();
super.onPreExecute();
}*/
#Override
protected Void doInBackground(Void... voids) {
try {
ConnectionHelper con = new ConnectionHelper();
conn = con.connectionclasss(Const.DBUserNameStr, Const.DBPasswordStr, Const.db, Const.ip);
String queryStmt = "Insert into CleaningTable" +
" (StartTime, StopTime, TimeTaken) values "
+ "('"
+ starttime
+ "','"
+ endtime
+ "','"
+ timedifference
+ "')";
Statement stmt = conn.createStatement();
isSucess = stmt.execute(queryStmt);
isSucess = true;
conn.close();
// return data;
} catch (Exception e) {
isSucess = false;
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
if (isSucess) {
Toast.makeText(getActivity(), "time added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "time not added", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(aVoid);
}
}
}

How do you find the time difference between onStop and onCreate lifecycle callbacks?

I am an absolute beginner and was trying to code a Count-up-timer app that will run in the background, in other words when the app is closed from overview or when the back button is pressed, the timer will still appear to continue on the corrected time the next time the app is opened. I tried to do this by using SharedPreferences.
An error that I run into is that when I launch the emulator, the timer does not start at 00:00:00 as it should, however, it starts at random times. Here is a screenshot
public class MainActivity extends AppCompatActivity {
TextView timerText;
TextView dayText;
Button startStopButton;
Timer timer;
TimerTask timertask;
Double time = 0.0;
Double mEndTime = 0.0;
Double startingSysTime = 0.0;
Double timeGap = 0.0;
public static final String SHARED_PREFS ="sharedPrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerText = (TextView) findViewById(R.id.timerText);
startStopButton = (Button) findViewById(R.id.startStopButton);
dayText = (TextView) findViewById(R.id.dayText);
timer = new Timer();
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
time = Double.longBitsToDouble(prefs.getLong("mTimeValue", Double.doubleToLongBits(0.0)));
mEndTime = Double.longBitsToDouble(prefs.getLong("onDestr_SysTime", Double.doubleToLongBits(0.0)));
if(mEndTime==0.0){
startingSysTime = 0.0;
}else{
startingSysTime = (double) (System.currentTimeMillis());
}
timeGap = startingSysTime - mEndTime;
time += timeGap;
startTimer();
}
#Override
protected void onStop() {
super.onStop();
Log.i("TIMEGAP", "onStop called");
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("mTimeValue", Double.doubleToRawLongBits(time)); //saves the time value
editor.putLong("onDestr_SysTime", Double.doubleToRawLongBits(System.currentTimeMillis())); //saves the CurrentSystemTime when onStop is invoked
editor.apply();
}
private void startTimer() {
timertask = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
time++;
timerText.setText(getTimerText());
dayText.setText(getDayText());
}
});
}
};
timer.scheduleAtFixedRate(timertask, 0, 1000);
}
private String getDayText() {
int rounded = (int) Math.round(time);
int days = (rounded / 86400);
return formatDay(days);
}
private String formatDay(int days) {
String pluralDays;
if (days == 1) {
pluralDays = " Day";
} else {
pluralDays = " Days";
}
return days + pluralDays;
}
private String getTimerText() {
int rounded = (int) Math.round(time);
int seconds = ((rounded % 86400) % 3600) % 60;
int minutes = ((rounded % 86400) % 3600) / 60;
int hours = ((rounded % 86400) / 3600);
return formatTime(seconds, minutes, hours);
}
private String formatTime(int seconds, int minutes, int hours) {
return String.format("%02d", hours) + " : " + String.format("%02d", minutes) + " : " + String.format("%02d", seconds);
}
EDIT: Problem fixed, I simply had to convert timeGap to seconds by dividing it by 1000
timeGap = (startingSysTime - mEndTime)/1000;
Try this way
Handler.postDelayed(new Runnable{
//Todo write your code here
handler.postdelayed(this,1000);
},1000);
Start where you need
long starttime;
start time = System.currentTimeMilisecond();
End where you need
long currtime = System.currentTimeMilisecond() - startime;
}
According to your question try to convert this millisecond in your format which you need. Store long value in shared preference if you want to manage this on app close also

Running a method every second starting on button press

I need a timer to start any time I press a button (on the button itself) that shows how many seconds it's been since it's pressed in real time. Whenever it's pressed again, timer is reset to 0 and starts incrementing again
I know this isn't the way to do it, the button works fine but the timer should be in onCreate? I'm not sure how this is supposed to work with a button
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadedImg = (ImageView) findViewById(R.id.imageView);
}
public void clickAsync(View view) {
new ImageDownloader().execute(downloadUrl);
int seconds = 0;
Button button = (Button) view;
button.setText("Seconds since clicked: " + seconds);
Timer timer = new Timer();
//each time button is clicked, time is reset to 0 and increments in real time
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
seconds = 0;
seconds++;
button.setText("Seconds since clicked: " + seconds);
}
}, 0, 1000);
}
}
Another easy way to do this is to use Handler
mHandler = new Handler();
Just call updateSec();method on click of a button it'll update sec in interval of one seconds
Runnable UpdateRunnable = new Runnable() {
#Override
public void run() {
updateSec();
}
};
public void updateSec() {
mSeconds++;
mHandler.postDelayed(UpdateRunnable, 1000);
}
Example
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSeconds = 0;
updateSec();//it'll update sec variable every second.
}
});
try this: use a handler
long startTime = 0;
long elapsedTime ;
//runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
int hours = minutes / 60;
seconds = seconds % 60;
//textview for displaying time..
timerTextView.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));
timerHandler.postDelayed(this, 1000);
}
};
b.setOnClickListener(new View.OnClickListener() { //b is your button
#Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("Stop")) {
elapsedTime = System.currentTimeMillis() - startTime;
timerHandler.removeCallbacks(timerRunnable);
b.setText("Resume");
} else {
startTime = System.currentTimeMillis() - elapsedTime;
timerHandler.postDelayed(timerRunnable, 0);
Calendar cs = Calendar.getInstance();
System.out.println("Current time => " + cs.getTime());
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
String formattedDate = df.format(cs.getTime());
timerTextView.setText(formattedDate);
b.setText("Stop");
}
}
});
it will calculate the elapsed time and show time after stop...
You can use threads:
#Override
public void onClick(View view){
switch(view.getId()){
case R.id.button:
new Thread(new Runnable() {
#Override
public void run() {
try{
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
count++;
textView.post(new Runnable() {
#Override
public void run() {
textView.setText(count + "");
}
});
}
}
}).start;
break;
}
}
the view must be updated on main thread, and so you need to use post() method that has runnable instance as parameter.
Alternatively, you can also use AsyncTask.

Why won't CountDownTimer start onTick when it should?

I've searched everywhere for an answer to this dilemma. A CountDownTimer(bsCountDownTimer) will not start when it should. When I click the button, it is supposed to begin the onTick(), correct? However, only after I navigate to a different activity and navigate backwards using the UP button, or exit the app or destroy it and restart it does the onTick() start updating the text and sending information to LogCat like it is told to do.
My best guess is that this problem is exclusive to either the CDT sub-class, onBsButtonClick(), onCreate(), or possible onResume()/onPause().
Here is some of the source code.
public class HomeActivity extends Activity {
#SuppressLint("NewApi") #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(bsTimeStamp!=0){bsIsRunning=true;}
if(bsTimeStamp==0){bsIsRunning=false;}
lastFed=(TextView)findViewById(R.id.lastFedTextView);
feedAt=(TextView)findViewById(R.id.feedAtTextView);
daysText = (EditText)findViewById(R.id.editTextDays);
hoursText = (EditText)findViewById(R.id.editTextHours);
minutesText = (EditText)findViewById(R.id.editTextMinutes);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
// get timeStamp and boolean from onFeedButtonClick() START
final SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
feedClickTimeStamp = prefs.getLong("savedOnFeedClick", placeholder);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
bsPlaceholder = prefs.getLong("saved placeholder", bsPlaceholder);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
progressbs = Long.toString(bsProgress);
placeholderbs = Long.toString(bsPlaceholder);
timestampbs = Long.toString(bsTimeStamp);
timestamp = Long.toString(feedClickTimeStamp);
LAST_FED = prefs.getString("lastFed", LAST_FED);
FEED_AT = prefs.getString("feedAt", FEED_AT);
feedAt.setText("Feed at: " + FEED_AT);
lastFed.setText("Last fed at: " + LAST_FED);
// get timeStamp and boolean from onFeedButtonClick() END
DateTime date = new DateTime();
long currentTime = date.getMillis();
Long bsDiffInMillis;
if(bsIsRunning=false) {
bsDiffInMillis = 0L;
}
else {
bsDiffInMillis = currentTime - bsTimeStamp;
bsPlaceholder -= bsDiffInMillis;
}
Integer bsDiffInt = Integer.valueOf(bsDiffInMillis.intValue());
int roundedDiff = (bsDiffInt + 500) / 1000 * 1000;
j += roundedDiff - 2000;
// BS PROGRESS BAR START
bsProgressBar = (ProgressBar)findViewById(R.id.bsProgressBar);
bsProgressBar.setProgress(j);
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
setProgressBarVisibility(true);
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
bsProgressBar.setRotation(180);
} else{
// FIND A WAY TO ROTATE PROGRESSBAR BEFORE API 11 (3.0)
}
timeDisplayBs=(TextView)findViewById(R.id.bs_countdown); ((TextView)findViewById(R.id.bs_countdown)).setText(convertMillisForCrafting(bsPlaceholder-ji));
millisInFuture = bsPlaceholder;
bsCountDownTimer = new CDT(millisInFuture, countDownInterval);
// START BS BUTTON LISTENER //
final Button startBsBtn = (Button) findViewById(R.id.bsButton);
startBsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBsButtonClick(view);
} //close onClick
}); //close onClickListener
// END BS BUTTON LISTENER //
if (savedInstanceState != null) {} else {}
}// onCreate END
#Override
protected void onPause() {
super.onPause();
bsCountDownTimer.cancel();
}
#SuppressLint("NewApi") #Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
if(feedClickTimeStamp>0){
mountCountDownTimer.start();
}
if(bsIsRunning==true) {
bsCountDownTimer.start();
bsIsRunning=true;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
}
if(bsIsRunning==false){
bsIsRunning=false;
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
}
}
public class CDT extends CountDownTimer {
public CDT(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
millisInFuture = prefs.getLong("saved placeholder", bsPlaceholder);
timeDisplayBs=(TextView)findViewById(R.id.bs_countdown);
}
#Override
public void onTick(long millisInFuture) {
Log.v("bsTimer", "Tick of Progress " + ji + " " + millisInFuture);
ji+=1;
j+=1000;
bsProgressBar.setProgress(j);
timeDisplayBs.setText(convertMillisForCrafting(millisInFuture-ji));
}
#Override
public void onFinish() {
bsCountDownTimer.cancel();
j=0;
ji=0;
bsPlaceholder = 0;
bsTimeStamp = 0;
bsProgressBar.setProgress(0);
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
}
}
public void onBsButtonClick(View view) {
final SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
bsPlaceholder = prefs.getLong("saved placeholder", bsPlaceholder);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
int daysPh;
int hoursPh;
int minutesPh;
String daysStr = daysText.getText().toString();
String hoursStr = hoursText.getText().toString();
String minutesStr = minutesText.getText().toString();
if (daysStr.matches("") && hoursStr.matches("") && minutesStr.matches("")) {
Toast.makeText(this, "You did not enter DAYS, HOURS, or MINUTES.", Toast.LENGTH_LONG).show();
return;
}
if(bsIsRunning==false){
bsPlaceholder = 0;
bsTimeStamp = 0;
bsIsRunning=true;
j=0;
bsProgressBar.setProgress(0);
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
if(daysText.getText().toString().equals("")){
daysText.setText("0");
}
if(hoursText.getText().toString().equals("")){
hoursText.setText("0");
}
if(minutesText.getText().toString().equals("")){
minutesText.setText("0");
}
daysPh = Integer.parseInt(daysText.getText().toString());
hoursPh = Integer.parseInt(hoursText.getText().toString());
minutesPh = Integer.parseInt(minutesText.getText().toString());
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
SharedPreferences.Editor editor = prefs.edit();
bsPlaceholder = getMillisForCrafting(daysPh, hoursPh, minutesPh);
millisInFuture = bsPlaceholder; //VITAL
DateTime dt = new DateTime();
bsProgress = dt.getMillis();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", bsProgress).commit();
editor.putLong("saved placeholder", bsPlaceholder).commit();
bsCountDownTimer.start();
} //close if bsIsRunning==false
else if(bsIsRunning==true){
view.invalidate();
new AlertDialog.Builder(HomeActivity.this)
.setTitle("New Blacksmithing Research Timer? (erases current)")
.setMessage("Are you sure you want to start a new timer? \n(Current timer will be erased.)")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bsPlaceholder = 0;
bsTimeStamp = 0;
bsCountDownTimer.cancel();
bsIsRunning=true;
j=0;
bsProgressBar.setProgress(0);
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
if(daysText.getText().toString().equals("")){
daysText.setText("0");
}
if(hoursText.getText().toString().equals("")){
hoursText.setText("0");
}
if(minutesText.getText().toString().equals("")){
minutesText.setText("0");
}
int daysPh = Integer.parseInt(daysText.getText().toString());
int hoursPh = Integer.parseInt(hoursText.getText().toString());
int minutesPh = Integer.parseInt(minutesText.getText().toString());
SharedPreferences.Editor editor = prefs.edit();
bsPlaceholder = getMillisForCrafting(daysPh, hoursPh, minutesPh);
DateTime dt = new DateTime();
bsProgress = dt.getMillis();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", bsProgress).commit();
editor.putLong("saved placeholder", bsPlaceholder).commit();
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
bsCountDownTimer.start();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}return;
}
public static Long getMillisForCrafting(int daysPh, int hoursPh, int minutesPh) {
Locale.getDefault();
DateTime bs = new DateTime();
daysPulled = daysPh;
hoursPulled = hoursPh;
minutesPulled = minutesPh;
final long nowInMillis = bs.getMillis();
long days = daysPulled * 86400000;
long hours = hoursPulled * 3600000;
long minutes = minutesPulled * 60000;
long millisToAddToNow = days + hours + minutes;
long futureDateInMillis = millisToAddToNow + nowInMillis;
long millisFromDate = futureDateInMillis - nowInMillis;
return millisFromDate;
}
public void onBsResetButtonClick(View view) {
final SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
new AlertDialog.Builder(this)
.setTitle("Reset Timer?")
.setMessage("Reset Blacksmithing Research timer? \n(Current timer will be erased.)")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
j=0;
bsProgressBar.setProgress(0);
bsCountDownTimer.cancel();
bsIsRunning=false;
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
bsPlaceholder = 0;
bsTimeStamp = 0;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", 0).commit();
editor.putLong("saved placeholder", 0).commit();
// CLEAR INPUT EDITTEXT AREAS
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
// CLEAR INPUT EDITTEXT AREAS
}})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
#SuppressLint("DefaultLocale")
public static String convertMillis(long milliseconds){
long seconds, minutes, hours;
seconds = milliseconds / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
minutes = minutes % 60;
Locale.getDefault();
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return(time);
}
#SuppressLint("DefaultLocale")
public static String convertMillisForCrafting(long milliseconds){
long seconds, minutes, hours, days;
seconds = milliseconds / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
days = hours / 24;
hours = hours % 24;
minutes = minutes % 60;
Locale.getDefault();
String timeBs = String.format("%02d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds);
return(timeBs);
}
#Override
protected void onDestroy() {
super.onDestroy(); // Always call the superclass
mProgressBar.destroyDrawingCache();
mountCountDownTimer.cancel();
// bsProgressBar.destroyDrawingCache();
// bsCountDownTimer.cancel();
// android.os.Debug.stopMethodTracing(); // Stop method tracing that the activity started during onCreate()
}
}
I got it!
The countDownTimer wouldn't start because the variable from onBsButtonClick() (bsPlaceholder) wasn't being passed into onCreate() correctly.
Here's what I did:
Declared a global variable
static long timeInput;
Created a sub-class that extends CountDownTimer:
public class bsTimer extends CountDownTimer {
public bsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisInFuture) {
Log.v("bsTimer", "Tick of Progress " + ji + " " + millisInFuture);
ji+=1;
j+=1000;
bsProgressBar.setProgress(j);
((TextView)findViewById(R.id.bs_countdown)).setText(convertMillisForCrafting(millisInFuture-ji));
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
}
Instantiated the countdown timer in onCreate();
bsCountDownTimer = new bsTimer(timeInput, 1000);
Then just made a new timer and started it in the onBsButtonClick() method!
bsCountDownTimer = new bsTimer(timeInput, 1000).start();

TextView timer seconds digits jumping

I have this code which simply displays a countdown clock to an event. I am having an issue with it with regards to the last seconds place jumping digits unlike a normal clock. Here is the code I am working with. Could someone please offer some guidance to where this issue may be. Kind regards.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Timer().schedule(new TimerTask() {
File sdcard = Environment.getExternalStorageDirectory();
File cfgFile = new File(sdcard, "TimeTillParis/config.txt");
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
try {
InputStream is = new FileInputStream(cfgFile);
Properties prop = new Properties();
prop.load(is);
int mmY = Integer.parseInt(prop.getProperty("YEAR"));
int mmM = Integer.parseInt(prop.getProperty("MONTH"));
int mmD = Integer.parseInt(prop.getProperty("DAY"));
int mmH = Integer.parseInt(prop.getProperty("HOUR"));
int mmC = Integer.parseInt(prop.getProperty("MINUTE"));
int mmS = Integer.parseInt(prop.getProperty("SECOND"));
Calendar cal = Calendar.getInstance();
Date today = new Date(System.currentTimeMillis());
cal.set(Calendar.YEAR, mmY);
cal.set(Calendar.MONTH, mmM);
cal.set(Calendar.DAY_OF_MONTH, mmD);
cal.set(Calendar.HOUR_OF_DAY, mmH);
cal.set(Calendar.MINUTE, mmC);
cal.set(Calendar.SECOND, mmS);
long milliseconds = (cal.getTimeInMillis() - today.getTime());
String mD="", mH="", mM="", mS="", boxText="";
mD += (milliseconds / (1000*60*60*24));
mH += (milliseconds / (1000*60*60) % 24);
mM += (milliseconds / (1000*60) % 60);
mS += (milliseconds / 1000 % 60);
boxText += "Next visit to Paris in \n\n" +
mD + "d " +
mH + "h " +
mM + "m " +
mS + "s";
TextView textView = (TextView) findViewById(R.id.textView_date_display);
textView.setText(boxText);
} catch (FileNotFoundException e) {
String cfgNotFound="";
cfgNotFound += "config.txt not found!";
TextView textView = (TextView) findViewById(R.id.textView_date_display);
textView.setText(cfgNotFound);
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}, 0, 100);
}
}
I prepared complete solution, let me know if you have some questions:
public class MainActivity extends Activity {
private static final int COUNTDOWN_INTERVAL = 1000;
MyTimer timer;
Calendar eventTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File sdcard = Environment.getExternalStorageDirectory();
File cfgFile = new File(sdcard, "TimeTillParis/config.txt");
try {
InputStream is = new FileInputStream(cfgFile);
Properties prop = new Properties();
prop.load(is);
int mmY = Integer.parseInt(prop.getProperty("YEAR"));
int mmM = Integer.parseInt(prop.getProperty("MONTH"));
int mmD = Integer.parseInt(prop.getProperty("DAY"));
int mmH = Integer.parseInt(prop.getProperty("HOUR"));
int mmC = Integer.parseInt(prop.getProperty("MINUTE"));
int mmS = Integer.parseInt(prop.getProperty("SECOND"));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, mmY);
cal.set(Calendar.MONTH, mmM);
cal.set(Calendar.DAY_OF_MONTH, mmD);
cal.set(Calendar.HOUR_OF_DAY, mmH);
cal.set(Calendar.MINUTE, mmC);
cal.set(Calendar.SECOND, mmS);
eventTime = cal;
} catch (FileNotFoundException e) {
String cfgNotFound="";
cfgNotFound += "config.txt not found!";
TextView textView = (TextView) findViewById(R.id.textView_date_display);
textView.setText(cfgNotFound);
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
super.onResume();
if(eventTime != null) {
long millisInFuture = (eventTime.getTimeInMillis() - System.currentTimeMillis());
timer = new MyTimer(this, millisInFuture, COUNTDOWN_INTERVAL);
timer.start();
}
}
#Override
protected void onPause() {
super.onPause();
if(timer != null) {
timer.cancel();
}
}
private static class MyTimer extends CountDownTimer {
TextView textView;
public MyTimer(Activity actitivy, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// findViewById is very expansive, so fined it only once
textView = (TextView) actitivy.findViewById(R.id.textView_date_display);
}
#Override
public void onFinish() {
}
#Override
public void onTick(long millisUntilFinished) {
SimpleDateFormat sdf = new SimpleDateFormat("'Next visit to Paris in \n\n'd'd 'h'h 'm'm 's's'");
String boxText = sdf.format(new Date(millisUntilFinished));
textView.setText(boxText);
}
}
}

Categories