I am showing time out seconds using Toast inside Countdown Timer, But I feel the display of Toast is actually lagging then the real time seconds, is there any better way to display message properly ?
CountDownTimer timer = new CountDownTimer(20000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Toast.makeText(getApplicationContext(), "Disabling Task, Please wait : " + millisUntilFinished/1000, Toast.LENGTH_SHORT).show();
}
#Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "Congratulations!! Time out", Toast.LENGTH_SHORT).show();
}
}.start();
The problem is toast are added to a queue and executed one after another. Your ticks are quicker than the Toast.LENGTH_SHORT which causes delay. You want to hold a reference to previous toast and cancel it before showing a new one.
Toast mToast = null;
CountDownTimer timer = new CountDownTimer(20000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(getApplicationContext(), "Disabling Task, Please wait : " + millisUntilFinished/1000, Toast.LENGTH_SHORT);
mToast.show();
}
#Override
public void onFinish() {
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(getApplicationContext(), "Congratulations!! Time out", Toast.LENGTH_SHORT);
mToast.show();
}
}.start();
Warning! This doesn't work on Android 2.3.
Related
In my application i should use CountDownTimer and for this I want use this library : CountdownView.
I want when lasted 5s show me Toast.
For example : I want show 17s countTimer, when this timer receive to 5s show me toast "just 5s".
I use this code, but show me ever 5s .
long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(5000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
});
I want just lasted 5s not ever 5s.
how can i it?
By modifying your code:
boolean toastShown = false;
long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
if(remainTime < 5000 && !toastShown) {
toastShown = true;
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
}
});
You can try this code also:
CountDownTimer countDownTimer = new CountDownTimer(17000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Toast.makeText(HomeActivity.this, "running" + millisUntilFinished, Toast.LENGTH_SHORT).show();
if (millisUntilFinished < 5000) {
Toast.makeText(HomeActivity.this, "5 sec left", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
Toast.makeText(HomeActivity.this, "Countdown Timer Finished", Toast.LENGTH_SHORT).show();
}
};
countDownTimer.start();
I think you should check how many seconds remains.
I rewrote your code:
long time1 = 17L * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
long remainTimeInSeconds = remainTime / 1000;
if (remainTimeInSeconds == 5) {
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
}
});
My Android app crashes when it reaches countdown 0. Below is the part of code related to it.
final CountDownTimer countdown=new CountDownTimer(60000, 1000){
public void onTick(long millisUntilFinished)
{
tvTime.setText((millisUntilFinished / 1000)+"'s");
}
public void onFinish()
{
try{
tvTime.setText("Time Over");
this.cancel();
Toast.makeText(getApplicationContext(),"Answer: "+ OriginalWord, Toast.LENGTH_LONG).show();
Intent i=new Intent(LastJumble.this,ScoreCard.class);
i.putExtra("username",username);
i.putExtra("totalQues", totalQues);
i.putExtra("count", count);
startActivity(i);
}
catch (Exception e) {
// TODO: handle exception
}
}
}.start();
this.cancel() will try to cancel the CountDownTimer. Since the timer is finished this will break. If you mean to call a cancel method in your outer class you should reference it as OuterClass.this.cancel() where OuterClass is the name of the class.
I need to save an object in parse. I want to use save() with timeout . If save() is not successful then it should use saveEventually(). Can I instead use saveEventually() with a timer if the timer expires before getting the callback from saveEventually() it should show a message.
userQuestions = new ParseObject("UserQuestions");
userQuestions.put("questionAsked", edt_askQuestion.getText().toString());
userQuestions.put("askedByUser", SignInActivity.pUser);
userQuestions.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
//cancel the timeout timer
countDownTimer.cancel();
Intent intent = new Intent(AActivity.this, DActivity.class);
intent.putExtra(Constants.Extras.INTENT_EXTRA_QUESTION, edt_askQuestion.getText().toString());
startActivity(intent);
}
});
//countdown timer for timeout
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
LogUtil.d("seconds remaining: ", String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
//if timer is triggered before saving object
Toast.makeText(AActivity.this, "Problem with net connectivity question will be saved when net is connected", Toast.LENGTH_SHORT).show();
}
}.start();
is this correct or is there any other way?
I am using a toast for Count Down Timer, so the toast should change it's text in every second. I use this to display the toast for exactly 1 second but i want the toast to repeat itself. Hope i make you understand.
toast = Toast.makeText(getApplicationContext(), text.getText().toString(), Toast.LENGTH_SHORT); toast.show();
Handler handler = new Handler();
handler.postDelayed
(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 1000);
This will show a new toast every second for exactly one second.
int count = 100; //Declare as inatance variable
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
final Toast toast = Toast.makeText(
getApplicationContext(), --count + "",
Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 1000);
}
});
}
}, 0, 1000);
run() is called after every second. so show toast there.
Handler handler = new Handler();
handler.postDelayed
(new Runnable() {
#Override
public void run() {
toast.cancel();
toast = Toast.makeText(getApplicationContext(), text.getText().toString(),Toast.LENGTH_SHORT);
toast.show();
}
}, 1000);
This page describes a way to keep the toast be shown indefinitely. So when you have the text view of the toast on hand, you may change the text as you like.
you have to learn more about android srvices
create java class extends from IntentService
override this function
#Override
protected void onHandleIntent(Intent intent) {
try {
Toast.makeText(context,"Click on Location button to find your bus !",Toast.LENGTH_LONG).show();
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
go to manifest an type
go to your launcher java class and
Intent intent = new Intent(this, Service_toast.class);
startService(intent);
====>> for more information about services vist android devloper :
https://developer.android.com/guide/components/services.html
I get notification with socket.io to start a countdown timer in my android app. I use a Handler to send data from the socket.io callbacks to the UI thread. Handler messages work perfect. But starting the countdown timer from the handler doesn't work (onTick() function won't called). If I start it with the UI element, everything is ok. What is the best way to do it, when the countdown timer is operated not from the UI thread?
mHandler = new Handler(){
public void handleMessage(Message inputMessage) {
mBattle = (Battle) inputMessage.obj;
switch (inputMessage.what) {
case NO_BATTLE:
System.out.println("got message NO_BATTLE");
break;
//.................
case START_BATTLE:
startCountdownTimer(mBattle.getCountdown());
System.out.println("got message START_BATTLE");
break;
}
}
and the countdown timer function:
private void startProgressBar(final int time){
if (time != 0) {
new CountDownTimer(time, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress " + i + " " +
+ millisUntilFinished);
i++;
}
#Override
public void onFinish() {
i=0;
}
}.start();
}
Did you print out the value of time when it's not working? If time < SystemClock.elapsedRealtime() + 1000, then onFinish will be called without calling the onTick.