countdowntimer with sound.is it possible? - android

I want to ask a question I have put the timer to count down from 10:00 min..I want it when it goes at 2 min to put a sound for 10 seconds like an error sound or alarm sound is that possible and if it is can I have some help plz??

Yes it's possible, all you have to do to check the remaining time and play the sound accordingly:
private static final int Two_Seconds = 120000;
public void startTimer(){
countDownTimer = new CountDownTimer(timeLeftInMilliseconds,10000) {
#Override
public void onTick(long millisUntilFinished) {
if(millisUntilFinished <= Two_Seconds){
// display your sound
}
timeLeftInMilliseconds = millisUntilFinished;
updateTimer();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUSE");
timerRunning = true;
}

countdownText = findViewById(R.id.countdown_text);
countdownButton = findViewById(R.id.countdown_button);
countdownButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startstop();
}
});
updateTimer();
}
public void startstop(){
if(timerRunning){
stopTimer();
}else {
startTimer();
}
}
public void startTimer(){
countDownTimer = new CountDownTimer(timeLeftInMilliseconds,1000) {
#Override
public void onTick(long millisUntilFinished) {
timeLeftInMilliseconds = millisUntilFinished;
updateTimer();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUSE");
timerRunning = true;
}
public void stopTimer (){
countDownTimer.cancel();
countdownButton.setText("START");
timerRunning = false;
}
public void updateTimer (){
int minutes = (int) timeLeftInMilliseconds / 60000;
int seconds = (int) timeLeftInMilliseconds % 60000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if(seconds <7)timeLeftText +=0;
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
}

Related

Countdowntimer cancel crashes the app when click on button

I am trying to create a countDownTimer but, whenever I click on the button the app crashes.
I want that if I click on player1 button player2 countdown should pause and vice versa for player2.
The Error I am getting:
Attempt to invoke virtual method 'void
android.os.CountDownTimer.cancel()' on a null object reference
Here is the Code
public class Timer extends AppCompatActivity {
Button player1, player2;
long incrementTime = 3000;
int time;
long timeinLong;
ImageButton pause;
CountDownTimer player1count, player2count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
player1 = findViewById(R.id.player1);
player2 = findViewById(R.id.player2);
Intent intent = getIntent();
time = intent.getIntExtra("time", 0);
timeinLong = time * 60000;
int minutes = (int) (timeinLong / 1000) / 60;
int seconds = (int) (timeinLong / 1000) % 60;
String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
player1.setText(timeFormatted);
player1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startCountDown(player1);
player2count.cancel();
}
});
player2.setText(timeFormatted);
player2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startCountDown2(player2);
player1count.cancel();
}
});
}
private void startCountDown(final Button button) {
player1count = new CountDownTimer(timeinLong, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timeinLong = millisUntilFinished;
updateCountDownText(button);
}
#Override
public void onFinish() {
timeinLong = 0;
updateCountDownText(button);
}
}.start();
}
private void startCountDown2(final Button button) {
player2count = new CountDownTimer(timeinLong, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timeinLong = millisUntilFinished;
updateCountDownText(button);
}
#Override
public void onFinish() {
timeinLong = 0;
updateCountDownText(button);
}
}.start();
}
private void updateCountDownText(Button button) {
int minutes = (int) (timeinLong / 1000) / 60;
int seconds = (int) (timeinLong / 1000) % 60;
String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
button.setText(timeFormatted);
if (timeinLong < 30000) {
button.setTextColor(Color.RED);
} else {
button.setTextColor(Color.BLACK);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (player1count != null) {
player1count.cancel();
}
}
}
player2count only gets initialized inside startCountDown2, which is only called inside player2.setOnClickListener. Hence, if you click on the player1 button before ever clicking on player2 button, you'll get a NullPointerException, since calling player2count.cancel() on a not-yet-initialized player2count is not allowed. To prevent this from happening, it would be appropriate to just check for null in this case:
if (player2count != null) {
player2count.cancel();
}
Just don't cancel timers unless they're intialized, it will bring NPE, so check if they're not null in both players as below
player1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startCountDown(player1);
if (player2count != null)
player2count.cancel();
}
});
player2.setText(timeFormatted);
player2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startCountDown2(player2);
if (player1count != null)
player1count.cancel();
}
});
you don't initialize that when click listener called
i fixed it for myself by add try catch on .cancel lines
or set
if (player1count != null)
...

Passing value to countdowntimer() in android

I have the user inputting the time in the countdown timer.
Right now, its fixed to 10sec. How can i change this to user input ?
CountDownTimer counter = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
Call this method where you take the input from the user:
public void startCountDown(long duration) {
CountDownTimer counter = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
}
You'll probably want to make your CountDownTimer variable global as well.

Custom Count Up Timer

I need a count up timer in my application. I browsed many forums about this subject, but I could not find anything. Actually I understood we can do this with chronometer, but I have 2 problem with chronometer:
I cannot using chronometer in Service because chronometer needs a layout.
I cannot initialize chronometer to count more than 1 hour.
My code is here:
stopWatch = new Chronometer (MainActivity.this);
startTime = SystemClock.elapsedRealtime();
stopWatch.start();
stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
#Override
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (countUp / 60) + ":" + (countUp % 60);
Log.i("t", asText);
}
});
You can use a countDownTimer in reverse and get the time elapsed.
long totalSeconds = 30;
long intervalSeconds = 1;
CountDownTimer timer = new CountDownTimer(totalSeconds * 1000, intervalSeconds * 1000) {
public void onTick(long millisUntilFinished) {
Log.d("seconds elapsed: " , (totalSeconds * 1000 - millisUntilFinished) / 1000);
}
public void onFinish() {
Log.d( "done!", "Time's up!");
}
};
To start the timer.
timer.start();
To stop the timer.
timer.cancel();
The sec,min and hr increments everytime the values hit 59,59,23 respectively. Each values are displayed in different views creating a digital stopwatch.
checkin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(final View view) {
checkin.setEnabled(false);
new CountDownTimer(300000000, 1000){
public void onTick(long millisUntilFinished){
sec++;
if(sec==59) {
min++;
sec=0;
}
if(min==59){
min=0;
hr++;
}
if(hr==23){
hr=00;
}
secView.setText(String.valueOf(sec));
minView.setText(String.valueOf(min));
hrView.setText(String.valueOf(hr));
}
public void onFinish(){
Snackbar.make(view, "Finish", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}.start();
}
});
Using RxJava, you can write
Disposable var = Observable
.interval(1, TimeUnit.SECONDS)
.subscribe(
time -> {
long minutes = time / 60;
long second = time % 60;
timer.setText("" + minutes + ":" + second);
});
you can stop the timer by using
var.dispose()
You can use this code to do so:
https://gist.github.com/MiguelLavigne/8809180c5b8fe2fc7403
/**
* Simple timer class which count up until stopped.
* Inspired by {#link android.os.CountDownTimer}
*/
public abstract class CountUpTimer {
private final long interval;
private long base;
public CountUpTimer(long interval) {
this.interval = interval;
}
public void start() {
base = SystemClock.elapsedRealtime();
handler.sendMessage(handler.obtainMessage(MSG));
}
public void stop() {
handler.removeMessages(MSG);
}
public void reset() {
synchronized (this) {
base = SystemClock.elapsedRealtime();
}
}
abstract public void onTick(long elapsedTime);
private static final int MSG = 1;
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
synchronized (CountUpTimer.this) {
long elapsedTime = SystemClock.elapsedRealtime() - base;
onTick(elapsedTime);
sendMessageDelayed(obtainMessage(MSG), interval);
}
}
};
}

CountDownTimer with Seekbar

Hi I have implemented CountDownTimer timer with SeekBar and it is working fine, my problem is i am not able to change the timer when i drag the seek bar how to achieve this.
Example: If i drag and increase the CountDownTimer it should increase the time like that decrease also.
CountDownTimer Code:
private void startTimer(final int minuti) {
countDownTimer = new CountDownTimer(60 * minuti * 1000 ,
500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
circularSeekbar.setMax(60 * minuti);
int barVal = 0;
barVal = ((int) (minuti * 60 ) - (int) (seconds));
circularSeekbar.setProgress(-barVal);
mTvTime.setText(String.format("%02d", seconds / 60) + ":"
+ String.format("%02d", seconds % 60));
Log.d("TIME", mTvTime.getText().toString());
}
#Override
public void onFinish() {
}
}.start();
}
Seekbar code:
public class CircleSeekBarListener implements
OnCircularSeekBarChangeListener {
#Override
public void onProgressChanged(CircularSeekBar circularSeekBar,
int progress, boolean fromUser) {
if (fromUser) {
}
}
#Override
public void onStopTrackingTouch(CircularSeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(CircularSeekBar seekBar) {
// TODO Auto-generated method stub
}
}
here is code
set your seekbar in onTick
timer = new CounterClass(150000, 1000);
timer.start();
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
// timeTextView.setText("Completed.");
this.cancel();
AppUtills.FROMTIME_FINISH = true;
finish();
}
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
caaryLong = millis;
String hms = String.format(
"%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millis)));
System.out.println(hms);
timeTextView.setText(hms);
}
}
Try this code.
public class MainActivity extends AppCompatActivity {
private int seconds = 120; // two min
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
circularSeekbar = (SeekBar) findViewById(R.id.circularSeekbar);
mTvTime = (TextView) findViewById(R.id.mTvTime);
circularSeekbar.setMax(seconds);
circularSeekbar.setProgress(seconds);
circularSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) {
seconds = progress;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
startTimer(seconds * 1000);
}
});
startTimer(seconds * 1000);
}
private void startTimer(final long milisecods) {
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(milisecods, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
int barVal = (int) leftTimeInMilliseconds / 1000;
circularSeekbar.setProgress(barVal);
mTvTime.setText(String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds),
TimeUnit.MILLISECONDS.toSeconds(leftTimeInMilliseconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds))
));
}
#Override
public void onFinish() {
}
}.start();
}
}

How can I pause the timer in android?

I have gone through the link http://dewful.com/?tag=basic-android-timer regarding the timer application in android. It is working fine. I need to add the pause button to stop the timer and a play button to start the timer again from where I stopped. Can I achieve that task?
My Code:
long timervalue = 50000;
CountDownTimer Counter1 = new CountDownTimer(timervalue, 1000)
{
public void onTick(final long millisUntilFinished)
{
time.setText(formatTime(millisUntilFinished));
pause.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.cancel();
}
}
);
resume.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Counter1.start();
timervalue = Long.parseLong(output);
System.out.println("paused timer value resumed"+timervalue);
Counter1.onTick(timervalue);
}
}
);
}
public void onFinish()
{
Counter1.cancel();
}
};
public String formatTime(long millis)
{
output = "00";
seconds = millis / 1000;
seconds = seconds % 60;
System.out.println("seconds here"+seconds);
String secondsD = String.valueOf(seconds);
System.out.println("secondsD here"+secondsD);
if (seconds <
10) secondsD = "0" + seconds;
System.out.println("secondsD here in if"+secondsD);
output = secondsD;
return output;
}
In the above code when resume button is clicked the timer is again starting from 50sec and I don't want like that. It should start from the time where I paused. Please help me regarding this...I am struggling for this since one week......
Will be really thankful for the help..........
public class TimerActivity extends Activity
{
EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
counter= new MyCount(5000,1000);
counter.start();
}
public void asdf(View v)
{
switch(v.getId())
{
case R.id.button1: counter.cancel();
break;
case R.id.button2: counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override public void onFinish()
{
e1.setText("DONE");
}
#Override public void onTick(long millisUntilFinished)
{
s1=millisUntilFinished;
e1.setText("left:"+millisUntilFinished/1000);
}
}
}
public void time(long m){
timer=new CountDownTimer(m, 1000) {
public void onTick(long millisUntilFinished) {
tv_timer.setText(formatTime(millisUntilFinished));
}
public void onFinish() {
tv_timer.setText("done!");
}
}.start();
}
i have used this for timer and i have put button for pause and use this
case R.id.bT_PAUSE:
String s_time = null;
try{
if(Bt_pause.getText().equals("PAUSE")){
s_time=tv_timer.getText().toString();
timer.cancel();
String[] Pause_time=s_time.split(":");
m=Long.parseLong(Pause_time[0].trim());
n=Long.parseLong(Pause_time[1].trim());
m=(m*60)+n;
m=m*1000;
Bt_pause.setText("RESUME");
}else if(Bt_pause.getText().equals("RESUME")){
//min_longmillis=Long.parseLong(sss);
//min_longmillis=min_longmillis*1000*60;
//min_longmillis=m;
//timer.start();
Toast.makeText(this,String.valueOf(m),Toast.LENGTH_SHORT).show();
time(m);
Bt_pause.setText("PAUSE");
}
}catch(Exception e){
Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
}
break;
and this for format time
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min= "0" + minutes;
output = min + " : " + sec;
return output;
}//formatTime

Categories

Resources