Count up timer on multiple activities - java

I have a count up timer in one of my activities and I want to show that timer with elapsed time in my 2nd activity. I tried to Use Fragments for this but the timer keeps restaring on the launch of 2nd activity.
My timer code is:
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
/* Timer.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));*/
customHandler.postDelayed(this, 0);
}
};

Pass the startTime from the first activity to the second in the Intent. In the second activity, don't set startTime to SystemClock.uptimeMillis() but instead read it from the extras in the Intent sent from the first activity.

Related

After set minute I see more than I set (countdownt timer)

Hi I am making countdown timer. And when I put value of minutes less than one minute everything works correctly. But when I put value for example 1:05 min and click start I see counting down from 1:44. And this problem always appears when I put value bigger than 1 minute
//rundowy czasomierz {
mCountDownTimer = new CountDownTimer(millisInput, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
tv4.setText((round_count[0]+1)+"/"+finalNum_rounds);
}
#Override
public void onFinish() {
round_count[0]++;
if (round_count[0]<finalNum_rounds){
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
start();
}
}, przerwasinput);
}
MediaPlayer ring = MediaPlayer.create(Main6Activity.this, R.raw.ring1);
ring.start();
tv3.setText("Przerwa");
if (!tv1.equals("00:00")) {
tv1.setText("Koniec");
}
private void updateCountDownText(){
int hours = (int) (mTimeLeftInMillis / 1000) / 3600;
int minutes = (int) ((mTimeLeftInMillis / 1000) % 3600) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted;
timeLeftFormatted = String.format(Locale.getDefault(),
"%02d:%02d", minutes, seconds);
tv1.setText(timeLeftFormatted);
}
My best guess is that your program interprets an input of 1:05 as 105 seconds. This is equal to 1 minute and 45 seconds, so your timer would begin counting down from 1:45.
I don't see where in your code you process input, so I don't know exactly what is causing this.

Update textview every x seconds to show notification in background

I have this code
public void LoadCurrentTime(long time_last) {
long millis_now = System.currentTimeMillis();
long time_till = millis_now - time_last;
String showTime;
int showHours, showMin, showSec;
if (time_till > 604800000) {
showTime = getString(R.string.long_ago);
} else {
//long longHours = (time_till / 1000 / 60 / 60);
//long longMin = (time_till / 1000 / 60 % 60);
//long longSec = (time_till / 1000 % 60);
//showHours = (int) longHours;
//showMin = (int) longMin;
//showSec = (int) longSec;
//showTime = String.format("%02d", showHours) + ":" + String.format("%02d", showMin) + ":" + String.format("%02d", showSec);
// v1.0^
long longsec = (time_till / 1000) % 60;
long longmin = (time_till / (1000 * 60)) % 60;
long longhours = (time_till / (1000 * 60 * 60)) % 24;
showHours = (int) longhours;
showMin = (int) longmin;
showSec = (int) longsec;
showTime = String.format("%02d", showHours) + ":" + String.format("%02d", showMin) + ":" + String.format("%02d", showSec);
}
TextView lastTime = findViewById(R.id.textView4);
I want to update a TextView content every second and show a notification, but the TextView update and the notification show only when open the app, so
How can I update the TextView and show notification every one second in background?
You can user Timer and TimerTask to for recurring tasks. Don't forget to call timer.cancel() at end of the program.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Call your method
LoadCurrentTime()
}
},0, 2000);

difference between countdown timer time with actual time in Android?

I have one countdown timer in my activity.
In which I have converted Mins into Millisecond.
Based on that Millisecond, I have managed to counter HH: mm: ss using CountDownTimer.
So, I want a difference between main time and remaining time.
For example, My main time is 00:10:00 and when I stop counter suppose my remaining time is 00:07:30. means I spend 2:30 seconds
An expected result I want is 150seconds.
Example Code:
public class MainActivity extends AppCompatActivity {
int duration;
TextView textView;
ProgressBar progressbar;
Button btn_stop, btn_resume;
CountDownTimer counter;
int i = 1;
int remainingDuration;
int countDownInterval = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tvSampleText);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_resume = (Button) findViewById(R.id.btn_resume);
// duration=81200000; //6 hours
duration = (int) TimeUnit.MINUTES.toMillis(1);
counter = new MyCount(duration, countDownInterval);
counter.start();
btn_stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
counter.cancel();
}
});
btn_resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
counter = new MyCount(remainingDuration, countDownInterval);
counter.start();
}
});
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
textView.setText("00:00:00");
}
#Override
public void onTick(long millisUntilFinished) {
i++;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long elapsedHours = millisUntilFinished / hoursInMilli;
millisUntilFinished = millisUntilFinished % hoursInMilli;
long elapsedMinutes = millisUntilFinished / minutesInMilli;
millisUntilFinished = millisUntilFinished % minutesInMilli;
long elapsedSeconds = millisUntilFinished / secondsInMilli;
String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
textView.setText(yy);
progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
remainingDuration = (int) millisUntilFinished;
}
}
}
First convert the minutes duration into seconds
long secs = minutes * 60; 10 * 60 = 600
and inside on tick, you get the remaining time in milliseconds so keep the value as global reference and convert it into seconds as
long mMillisUntilFinished;
void onTick (long millisUntilFinished){
// mMillisUntilFinished = 450000
mMillisUntilFinished = millisUntilFinished;
}
then when you press stop then get the renaming time difference as
long diffSeconds = secs - (mMillisUntilFinished / 1000)
// 600 - (450000 /1000)
// 600 - 450
// 150
update: As suggested, calculate the seconds for both duration and left time then subtract them to get the result.
Use long instead of int casting
#Override
public void onTick(long millisUntilFinished) {
i++;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long tempMili = millisUntilFinished;
long elapsedHours = tempMili / hoursInMilli;
tempMili = tempMili % hoursInMilli;
long elapsedMinutes = tempMili / minutesInMilli;
tempMili = tempMili % minutesInMilli;
long elapsedSeconds = tempMili / secondsInMilli;
String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
textView.setText(yy);
progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
remainingDuration = (duration/ 1000) - (millisUntilFinished/1000);
}

Timer adds a certain TableRow multiple times

I have a basic Timer in my app:
TextView timerTextView = (TextView) findViewById(R.id.timer);
long startTime = 0;
Handler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000) * 1;
int minutes = seconds / 60;
seconds = seconds % 60;
timerTextView.setText(String.format("%02d:%02d", minutes, seconds));
timerHandler.postDelayed(this, 500);
if (minutes == 45 && seconds == 0) {
addHeader("2nd Half");
}
}
};
addHeader() method:
private void addHeader(String header) {
TableLayout table = (TableLayout) findViewById(R.id.myTable);
TableRow tr = new TableRow(this);
tr.setLayoutParams(...);
TextView textView = new TextView(this);
textView.setLayoutParams(..);
textView.setText(header);
// more code
tr.addView(textView);
table.addView(tr);
}
onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTimer();
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
}
When my timer reaches 45:00 a new row is added to my TableLayout multiple times. I've created an alternative solution but I'd like to know what I'm missing.
if (!isAdded) {
addHeader("2nd Half");
isAdded = true;
}
A good way to check what is going on is to log every time the Handler is run and every time addHeader fires
e.g:
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000) * 1;
int minutes = seconds / 60;
seconds = seconds % 60;
millis = millis % 1000;
timerTextView.setText(String.format("%02d:%02d", minutes, seconds));
Log.d("MainActivity", String.format(Locale.ENGLISH,"Checking time: %02d:%02d:%03d" , minutes, seconds, millis));
timerHandler.postDelayed(this, 500);
if (minutes == 45 && seconds == 0) {
Log.d("MainActivity", String.format(Locale.ENGLISH,"2nd Half: %02d:%02d:%03d" , minutes, seconds, millis));
addHeader("2nd Half");
}
}
Doing this you will see that addHeader is called twice. This is because every 500ms your Handler is running, checking the difference between the current time and start time, in minutes and seconds (ignoring ms). To fix the issue try changing
timerHandler.postDelayed(this, 500);
to
timerHandler.postDelayed(this, 1000);

Change a TextView when my Timer reaches a random time

Here is what I have so far
long startTime = 0;
TextView timerTextView;
final Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) millis / 1000;
int minutes = (int) seconds / 60;
seconds %= 60;
int min = 40000;
int max = 49999;
long randomTime = (long) (Math.random()*(min-max) + max);
timerTextView.setText(minutes + ":" + seconds);
timerHandler.postDelayed(this, 500);
}
};
I want to know how I can use the randomTime variable to trigger my TextView to change to say Time Is Up. I found a way to create a scheduled task using the following:
timerHandler.postAtTime(Runnable r, randomTime);
The only problem is I don't know what Runnable I should use to cause the TextView to change.
First of all put the randomTime outside of the thread,
int min = 40000;
int max = 49999;
long randomTime = (long) (Math.random()*(min-max) + max);
Create a simple handler that'll run after the time specified by randomTime variable,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// handler has reached the time
// now update the TextView using a runOnUiThread
runOnUiThread(new Runnable() {
public void run() {
// Update UI elements
// get the current time now
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) millis / 1000;
int minutes = (int) seconds / 60;
seconds %= 60;
//finally update the TextView
timerTextView.setText(minutes + ":" + seconds);
}
});
}
}, randomTime);

Categories