Unable to use String variable outside if statement - java

I have implemented timer like this (Mentioned in code). I changed its string format so that it takes hours:minutes:seconds and for me, to use a switch, I have to add an if(){} conditional statement.
So I did and I had to declare the string inside the if(){} cause i couldnt do it outside if(){}. But now I want to use that string information for an intent to open a new activity and assign the string value to a TextView.
The problem is I can't use the string outside of the if(){} and at first I used the hms string with a TextView called myText.
The thing is that the myText TextView worked perfectly on the first activity but when sending the information using Inetnt to another Textview in the other activity it showed no change on the TextView.
Heres is the code:
if (mySwitch.isChecked()) {
int getvaluehour = numPickerHour.getValue();
int getvalueminute = numPickerMin.getValue();
getvaluehour = getvaluehour * 3600000;
getvalueminute = getvalueminute * 60000;
long hoursandMinstomils = getvalueminute + getvaluehour;
new CountDownTimer(hoursandMinstomils, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
myText.setText(hms);
}
public void onFinish() {
myText.setText("TIME'S UP!!"); //On finish change timer text
}
}.start();
and heres how i used the Intent Activity1:
Intent toST = new Intent(MainActivity.this, ShowTime.class);
String textMessage = myText.getText().toString() ;
toST.putExtra("wargra", textMessage);
startActivity(toST);
and heres how I received it in activity2:
Bundle receiver = getIntent().getExtras();
if(receiver == null){
return;
}
String textMessage = reciver.getString("wargra");
myText2.setText(textMessage);
How can I send hms value to myText2?
Here is the complete activity1 code:
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Switch;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
Button silentButton;
AudioManager myAudioManager;
NumberPicker numPickerHour;
NumberPicker numPickerMin;
Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Text,Audio,Button,Time
myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
silentButton = (Button) findViewById(R.id.Start);
//Number Picker
numPickerHour = (NumberPicker) findViewById(R.id.numberPickerHour);
numPickerMin = (NumberPicker) findViewById(R.id.numberPickerMinute);
mySwitch = (Switch) findViewById(R.id.Toggle);
//Hour
numPickerHour.setMaxValue(24);
numPickerHour.setMinValue(0);
numPickerHour.setWrapSelectorWheel(true);
//Min
numPickerMin.setMaxValue(60);
numPickerMin.setMinValue(0);
numPickerMin.setWrapSelectorWheel(true);
silentButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
final TextView myText;
myText = (TextView)findViewById(R.id.timeText);
if (mySwitch.isChecked()) {
int getvaluehour = numPickerHour.getValue();
int getvalueminute = numPickerMin.getValue();
getvaluehour = getvaluehour * 3600000;
getvalueminute = getvalueminute * 60000;
long hoursandMinstomils = getvalueminute + getvaluehour;
new CountDownTimer(hoursandMinstomils, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("Viberation end in: " + "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
myText.setText(hms);
//set text
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
public void onFinish() {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
myText.setText("TIME'S UP!!"); //On finish change timer text
}
}.start();
} else {
int getvaluehour = numPickerHour.getValue();
int getvalueminute = numPickerMin.getValue();
getvaluehour = getvaluehour * 3600000;
getvalueminute = getvalueminute * 60000;
long hoursandMinstomils = getvalueminute + getvaluehour;
new CountDownTimer(hoursandMinstomils, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("Silent ends in (" + "%02d:%02d:%02d" + ")", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
myText.setText(hms);//set text
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
public void onFinish() {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
myText.setText("TIME'S UP!!"); //On finish change timer text
}
}.start();
}
Intent toST = new Intent(MainActivity.this, ShowTime.class);
String textMessage = myText.getText().toString() ;
toST.putExtra("wargra", textMessage);
startActivity(toST);
}
}
);
}
}

final TextView myText;
myText = (TextView)findViewById(R.id.timeText);
Change these two lines. Declare myText globally (as you have already done with silentButton, myAudioManager, numPickerHour, numPickerMin, mySwitch).
public class MainActivity extends AppCompatActivity {
//Following your other code
....
TextView myText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Following your other code
....
myText = (TextView)findViewById(R.id.timeText);
//Rest of your code
....
}
}
Hope your problem will be gone.

There could be two reasons and solutions:
case 1: If you are using your if inside a Thread then you should update the TextView on UIThread i.e MainThread.
Like this:
if (mySwitch.isChecked()) {
int getvaluehour = numPickerHour.getValue();
int getvalueminute = numPickerMin.getValue();
getvaluehour = getvaluehour * 3600000;
getvalueminute = getvalueminute * 60000;
long hoursandMinstomils = getvalueminute + getvaluehour;
new CountDownTimer(hoursandMinstomils, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
MainActivity.this.runOnUIThread(new Runnable()
{
#Override
public void run()
{
myText.setText(hms);
}
}).start();
}
public void onFinish() {
MainActivity.this.runOnUIThread(new Runnable()
{
#Override
public void run()
{
myText.setText("TIME'S UP!!"); //On finish change timer text
}
}).start();
}
}.start();
You should always update any UI element on MainThread only by using the runOnUIThread() method. May be this was the cause of your problem.
case 2: Solution given by Sudip Podder.
case 3 : Answer to your query :
In java you can not just use a variable outside of its scope. All you can do the thing is to put the value to class variable like this :
Just declare a class variable and put value in it and then use the class variable for the values.
public class MainActivity extends AppCompatActivity {
....
....
String time;
....
....
}
....
....
....
if (mySwitch.isChecked()) {
int getvaluehour = numPickerHour.getValue();
int getvalueminute = numPickerMin.getValue();
getvaluehour = getvaluehour * 3600000;
getvalueminute = getvalueminute * 60000;
long hoursandMinstomils = getvalueminute + getvaluehour;
new CountDownTimer(hoursandMinstomils, 1000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
MainActivity.this.time=hms;
MainActivity.this.runOnUIThread(new Runnable()
{
#Override
public void run()
{
myText.setText(MainActivity.this.time);
}
}).start();
}
public void onFinish() {
MainActivity.this.runOnUIThread(new Runnable()
{
#Override
public void run()
{
myText.setText("TIME'S UP!!"); //On finish change timer text
}
}).start();
Now you can use the value of hms variable by the class variable time. And yes don't forget to declare myText as class variable.

Related

Change text from a class out

I created a simple Java class. This should spend the time. With System.out.println it works too! But how can I inform a certain TextView that it should also change?
Countdown_Test.java
public class Countdown_test {
private static long START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
//private TextView timer;
public Countdown_test(long start_time) {
START_TIME_IN_MILLIS = start_time;
mTimeLeftInMillis = START_TIME_IN_MILLIS;
//timer = findViewById(R.id.timer); dosen't work
}
public void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
}
}.start();
mTimerRunning = true;
}
public void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
System.out.println(timeLeftFormatted);
}
}
MainActivity.java
Countdown_test ct = new Countdown_test(600000);
button_start.setOnClickListener(v -> {
ct.startTimer();
});
ct.updateCountDownText();
You could write an interface, a TimeLeftListener with a method onTimeLeftChanged(String timeleft). Then your activity extends TimeLeftListener and implements the onTimeLeftChanged()-method.
This is where you update your views.
When you initiate your CountDownTest you pass the activity like this.
Countdown_test ct = new Countdown_test(600000, this);
And create al listener in you CountDownTest-class like this
myTimeLeftListener =(TimeleftListener)activity;
Then in your updateCountDownText()-Method you put
myTimeLeftListener.onTimeLeftChanged(timeleft)
You could also use ViewModel and LiveData.

Count down timer how to implement onfinish method

I have a countdown timer that i want to implement on finish method or some kind of code so that when the timer stops, the text views change to Time's up and another method is initiated (in the activity).
To clarify, the timer is given a starting number that counts down from, to zero in format of xx:xx.
The class of the timer :
public class countdown_timer {
private long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
this.millisInFuture = pMillisInFuture;
this.countDownInterval = pCountDownInterval;
this.pls = pMillisInFuture;
status = false;
Initialize();
}
public void Stop() {
status = false;
}
public void Reset() {
millisInFuture = pls;
}
public long getCurrentTime() {
return millisInFuture;
}
public void Start() {
status = true;
}
public void Initialize()
{
final Handler handler = new Handler();
Log.v("status", "starting");
final Runnable counter = new Runnable(){
public void run(){
long sec = millisInFuture/1000;
if(status) {
if(millisInFuture <= 0) {
Log.v("status", "done");
} else {
Log.v("status", Long.toString(sec) + " seconds remain");
millisInFuture -= countDownInterval;
handler.postDelayed(this, countDownInterval);
}
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
handler.postDelayed(this, countDownInterval);
}
}
};
handler.postDelayed(counter, countDownInterval);
}
The activty that the timer is used:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_game_2);
//...find views
mycounterup = new countdown_timer(startcard, 1000);
mycounterdown = new countdown_timer(startcard, 1000);
RefreshTimer();
mycounterdown.Start();
public void RefreshTimer()
{
final Handler handler = new Handler();
final Runnable counter = new Runnable(){
public void run(){
int minutes_up_start = (int) (mycounterup.getCurrentTime() / 1000) / 60;
int seconds_up_start = (int) (mycounterup.getCurrentTime() / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
card_2_up.setText(time_2_up_start_formatted);
int minutes_down_start = (int) (mycounterdown.getCurrentTime() / 1000) / 60;
int seconds_down_start = (int) (mycounterdown.getCurrentTime() / 1000) % 60;
String card_2_down_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_down_start, seconds_down_start);
card_2_down.setText(card_2_down_start_formatted);
handler.postDelayed(this, 100);
}
};
handler.postDelayed(counter, 100);
}
You can use CountDownTimer:
new CountDownTimer(endsIn * 1000, 1000) {
public void onTick(long millisUntilFinished) {
timerTextView.setText(String.valueOf(millisUntilFinished/1000);
}
public void onFinish() {
}
}.start();
OR:
extend CountDownTimer class:
public class countdown_timer extends CountDownTimer {
TextView textView;
#Override
public void onTick(long millisInFuture) {
long sec = millisInFuture/1000;
if(millisInFuture <= 0) {
Log.v("status", "done");
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
}
}
#Override
public void onFinish() {
if(textView != null){
// change text in your textview
}
}
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
}
public countdown_timer(TextView textView, long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
this.textView = textView;
}
}
here is a two constructors, one of them is the same as is in your example and in second one you can pass also TextView object and use it in onFinish() method.
UPDATE 2:
Here is CountDownTimer in the Activity:
public class MainActivity extends AppCompatActivity {
TextView textView;
CountDownTimer mycounterdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startcard = 10000;
textView = (TextView) findViewById(R.id.test);
mycounterdown = new CountDownTimer(startcard, 1000) {
#Override
public void onTick(long mycounterup) {
int minutes_up_start = (int) (mycounterup / 1000) / 60;
int seconds_up_start = (int) (mycounterup / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
textView.setText(time_2_up_start_formatted);
}
#Override
public void onFinish() {
// call here other methods from activity
testMethod();
}
};
mycounterdown.start();
}
public void testMethod(){
Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
}
}
UPDATE 3: if last tick is one, not zero change count down interval to 500 instead of 1000:
public class MainActivity extends AppCompatActivity {
TextView textView;
CountDownTimer mycounterdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startcard = 10000;
textView = (TextView) findViewById(R.id.test);
mycounterdown = new CountDownTimer(startcard, 500) {
#Override
public void onTick(long mycounterup) {
int minutes_up_start = (int) (mycounterup / 1000) / 60;
int seconds_up_start = (int) (mycounterup / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
textView.setText(time_2_up_start_formatted);
}
#Override
public void onFinish() {
// call here other methods from activity
testMethod();
}
};
mycounterdown.start();
}
public void testMethod(){
Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
}
}
NOTE: take a look at this answer
First, extend CountDownTimer in your timer class.
public class countdown_timer extends CountDownTimer {
}
This allows you to implement some methods.
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
}
Also you must implement constructor that matches super class. You can also add some additional parameters. For example TextView
TextView textView;
public countdown_timer(long millisInFuture, long countDownInterval, TextView txt) {
super(millisInFuture, countDownInterval);
textView = txt;
}
The onFinish() is what you want. Also make sure you are using this class as a CountDownTimer. Then you will be able to start your timer.
Hope it helps.

Countdown timer wont display onTick

I'm having an issue where my code won't display the onTick method, and each time the counter is run, it simply displays the text "Done", as it should when it's finished. It seems that the endTime variable isn't being passed to the timer class? Any help with what I'm missing would be appreciated.
public class MainActivity extends AppCompatActivity {
private long endTime;
private MyTimer mainTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set up the two number pickers
NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
pickerSetup(minutes);
NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
pickerSetup(seconds);
mainTimer = new MyTimer(this, endTime, 1000);
}
public void pickerSetup(NumberPicker pickerId){
pickerId.setMaxValue(60);
pickerId.setMinValue(0);
pickerId.setWrapSelectorWheel(true);
}
public void calcTime(View view){
//Get value of Number pickers and convert to milliseconds from minutes and seconds.
NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
int selectedMinutes = minutes.getValue() * 60000;
NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
int selectedSeconds = seconds.getValue() * 1000;
//Add selected Minutes and seconds together
endTime = selectedMinutes + selectedSeconds;
Log.v("End time = ", String.valueOf(endTime));
mainTimer.start();
}
}
timer class:
class MyTimer extends CountDownTimer {
private Context mContext;
private TextView mToUpdate;
public MyTimer(Context context, long startTime, long interval) {
super(startTime, interval);
mContext = context;
TextView toUpdate = (TextView) ((Activity)mContext).findViewById(R.id.intervalTimer);
mToUpdate = toUpdate;
}
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
mToUpdate.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));
}
#Override
public void onFinish() {
mToUpdate.setText("Done");
}
}
It happens because you are not initialized endTime when the time of calling mainTimer = new MyTimer(this, endTime, 1000); . It is set to 0 and that is why it stops as starts. You can change the two methods into something like this.
public long calcTime(View view){
//Get value of Number pickers and convert to milliseconds from minutes and seconds.
NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
int selectedMinutes = minutes.getValue() * 60000;
NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
int selectedSeconds = seconds.getValue() * 1000;
//Add selected Minutes and seconds together
endTime = selectedMinutes + selectedSeconds;
Log.v("End time = ", String.valueOf(endTime));
return mainTimer.start();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set up the two number pickers
NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
pickerSetup(minutes);
NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
pickerSetup(seconds);
endTime=calcTime();
mainTimer = new MyTimer(this, endTime, 1000);
mainTimer.start();
}

How to stop a runnable timer at a specific condition?

I want to make a Timer Application which plays an alarm after a specific time has elapsed. I am new to Android Development so i picked up an example timer code from the Internet and am trying to modify it to my needs.
I have completed part where the timer plays the alarm when 1 minute has elapsed, but the alarm sound does not stop how can i do that.
My code is given below:
package com.example.aditya.timerapp;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity {
private ProgressBar progress;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
int progressStatus = 0;
private boolean stopped = false;
#Override
public void onCreate(Bundle savedInstanceState) {
//final CountDown timer = new CountDown();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValue = (TextView) findViewById(R.id.timerVal);
Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//timer.start();
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
Button pauseButton = (Button) findViewById(R.id.stopButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
/*try {
timer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
Button resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//timeSwapBuff += timeInMilliseconds;
timeInMilliseconds = 0L;
timeSwapBuff = 0L;
updatedTime = 0L;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.removeCallbacks(updateTimerThread);
onStop();
}
});
progress = (ProgressBar) findViewById(R.id.progressBar);
}
/*public void stopRun() {
//if (updatedTime == 0) {
//Toast.makeText(new MainActivity(), "StopRun",Toast.LENGTH_LONG).show();
timeSwapBuff = 0;
timerValue.setText("00:00:000");
//updateTimerThread.
customHandler.removeCallbacks(updateTimerThread);
//}
}*/
private Runnable updateTimerThread = new Runnable() {
#Override
public void run() {
//long totalMilliseconds = 1500000;
//while (!stopped){
//long totalMilliseconds = 15000;
//updatedTime = totalMilliseconds - SystemClock.currentThreadTimeMillis();
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
if (mins == 1 && secs == 0) {
playTimer();
}
}
};
public ProgressBar getProgress() {
return progress;
}
public void setProgress(ProgressBar progress) {
this.progress = progress;
}
public void playTimer(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), notification);
ring.play();
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
protected void onStop() {
customHandler.removeCallbacks(updateTimerThread);
super.onStop();
}
}
So now i need a method to stop the alarm when the user presses the Reset Button. Please suggest.
One way to do it is as follows:
Define a boolean inside your Runnable and check it before doing anything in run().
Flip it using a method inside your Runnable.
Here is the code snippet:
private Runnable updateTimerThread = new Runnable() {
private volatile boolean flag = true;
public void stopTimer() {
this.flag = false;
}
#Override
public void run() {
while(flag) {
//long totalMilliseconds = 1500000;
//while (!stopped){
//long totalMilliseconds = 15000;
//updatedTime = totalMilliseconds - SystemClock.currentThreadTimeMillis();
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
if (mins == 1 && secs == 0) {
playTimer();
}
}
}
};
Now, you just have to call stopTimer() to stop the Runnable.
Another way could be to handle the InterruptedException in the Runnable and send an interrupt from the main program.
If I understood question right,you can try to stop alarm sound by declaring your private Ringtone ring and call ring.stop() method in your onStop() method.
private Ringtone ring;
//...//
public void playTimer(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ring = RingtoneManager.getRingtone(getApplicationContext(), notification);
ring.play();
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
protected void onStop() {
customHandler.removeCallbacks(updateTimerThread);
ring.stop();
super.onStop();
}

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();

Categories