Unlimited interval for CountDownTimer - java

in this below simple code i want to set unlimited interval for CountDownTimer. i can not find any document for this action.
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
countDownTimer.cancel();
timerHasStarted = false;
}
#Override
public void onTick(long millisUntilFinished) {
Random rand = new Random();
int rnd = rand.nextInt(1000000000);
text.setText("" + rnd);
}
}
i want to stop that by click on button to finish.

In order to make the CountDownTimer infinite you can restart it onFinish method
like this
public void onFinish() {
if (!finishBtnPressed) {
countDownTimer.start();
} else {
//your logic
}
}
CountDownTimer has a method cancel() and you can call it when finish button is clicked (Note that cancel() call onFinish() so that's why i added the boolean variable finishBtnPressed). If you want to have some alternative then look into TimerTask

Related

How To Destroy CoundownTimer in Java when the button is pressed?

I make a timer with a count time of 5 seconds, then when I press the exit button the counter automatically stops?
Here is my timer code:
public void startTimer(final long finish, long tick) {
CountDownTimer t;
t = new CountDownTimer(finish, tick) {
public void onTick(long millisUntilFinished) {
long remainedSecs = millisUntilFinished / 1000;
textTimer.setText("" + (remainedSecs / 60) + ":" + (remainedSecs % 60));// manage it accordign to you
}
public void onFinish() {
textTimer.setText("00:00");
Toast.makeText(FloatingVideoWidgetShowService.this, "Waktu Habis", Toast.LENGTH_SHORT).show();
long seek = videoView.getCurrentPosition();
videoView.setKeepScreenOn(false);
stopSelf();
WritableMap args = new Arguments().createMap();
args.putInt("index", index);
args.putInt("seek", (int) seek);
args.putString("url", playingVideo.getString("url"));
args.putString("type", "close");
sendEvent(reactContext, "onClose", args);
onDestroy();
cancel();
}
}.start();
}
And this is my code when pressing the stop / exit button :
floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
long seek = videoView.getCurrentPosition();
videoView.setKeepScreenOn(false);
stopSelf();
WritableMap args = new Arguments().createMap();
args.putInt("index", index);
args.putInt("seek", (int) seek);
args.putString("url", playingVideo.getString("url"));
args.putString("type", "close");
sendEvent(reactContext, "onClose", args);
onDestroy();
}
});
How so when btn_deny is clicked Cuntdowntimer stops and does not force close?
Thanks.
You can't use onDestroy() to close your activity or fragment. Instead, you need to call finish().
To close the CountDownTimer, you need to make it a class scope variable. Prepare the timer at your startTimer then stop the timer by calling t.cancel() like the following code:
public class YourActivity extends Activity {
// Declare the variable to be accessed later.
CountDownTimer t;
...
public void startTimer(final long finish, long tick) {
t = new CountDownTimer(finish, tick) {
...
}.start();
}
private void yourOtherMethod() {
floatingWindow.findViewById(R.id.btn_deny).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(t != null) t.cancel();
...
}
});
}
}

Auto next activity with countdown timer not working with cancel method

I try auto next activity with countdown timer but not work if use cancel method.
//Next Activity
CountDownTimer myCountDownTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent startActivity = new Intent(ActivityNew1.this,ActivityNew2.class);
startActivity(startActivity);
}
}.start();
myCountDownTimer.cancel();
Above java code auto next activity every 1s not working. And I try remove myCountDownTimer.cancel();
//Next Activity
new CountDownTimer(30000, 1000) {
public void onFinish() {
Intent startActivity = new Intent(ActivityNew1.this,ActivityNew2.class);
startActivity(startActivity);
finish();
}
public void onTick(long millisUntilFinished) {
}
}.start();
This java code work for auto next activity but auto next activity run after back to home, auto next not stop.
Solved
I just Add this code on backpress method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent (this, MainActivity.class);
i.setFlags( Intent.FLAG_ACTIVITY_NO_HISTORY | FLAG_ACTIVITY_CLEAR_TOP );
startActivity( i );
finish();
finishAffinity();
System.exit( 0 );
}
Initialize a global boolean variable
boolean isPause = false;
Declare countdown timer
MyCountDownTimer myCountDownTimer;
Initialize CountDownTimer
myCountDownTimer = new MyCountDownTimer(5000, 1000);
myCountDownTimer.start();
Countdown Timer code
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
if(isPause){
startActivity(new Intent(LoginActivity.this, ForgotPasswordActivity.class));
myCountDownTimer.cancel();
}
}
#Override
public void onFinish() {
// startActivity(new Intent(ConsumerHomeActivity.this, ForgotPasswordCustomerActivity.class));
}
}
When you want to cancel the timer, just do this
isPause = true;
and then the code in onTick will hit and you will be in next activity as countdown timer does not invoke onFinish method.

ScrollView block some action

I have a ScrollView which englobes some TextView.
When I press on a textView there's a "MotionEvent.ACTION_UP/DOWN" method which make a textSize change, then after 2s the size text change again.
Handler handler5 = new Handler();
handler5.postDelayed(new Runnable() {
#Override
public void run() {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);
}
}, 2000);
With the ScrollView, the text doesn't change again after 2 seconds, any ideas?
You can use CountDownTimer Class like this:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
}
#Override
public void onTick(long millisUntilFinished) {
Presence.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);
}
}
Then call that class like this:
private CountDownTimer countDownTimer = new MyCountDownTimer(startTime, interval);

Countdown timer return the value of boolean

I have a Countdown timer and I want to return value when i call the class here is my class CountdownTimer
public class CountDownTimerActivity extends CountDownTimer {
public boolean finish = false;
public CountDownTimerActivity(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish()
{
finishTime(true);
}
#Override
public void onTick(long millisUntilFinished) {
finishTime(false);
Log.e("TESTINg","" + millisUntilFinished/1000);
}
public boolean finishTime(boolean finish) {
return finish;
}
}
Here is my calling countdown timer
CountDownTimerActivity countdowntimer = new CountDownTimerActivity(5000,1000);
countdowntimer.start();
if(// I DONT KNOW WHAT WILL I PUT HERE)
{
Log.e("Testing", "OK na");
}
Anyone can help me? Thank you
I think what you are trying to accomplish is a callback when the timer expires? If so, you have to think about the timer running by itself and then calling another method when finished. For instance:
public class Main extends Activity
{
private MyCountDownTimer myCountDownTimer;
#Override
public void onCreate(Bundle savedInstanceState){
myCountDownTimer = new MyCountDownTimer(5000, 1000);
myCountDownTimer.start();
}
public void finished(){
Log.e("Testing", "OK na");
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
finished();
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
}
}
See this link for more details/example:
https://androidcookbook.com/Recipe.seam;?recipeId=1205
EDIT to make a universal class that can be used in other activities, I would do something like this.
Create a MyCountDownTimer class that looks like this in its own file:
public class MyCountDownTimer extends CountDownTimer {
private MyCallback myCallback;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
public Interface MyCallBack{
void callback();
}
#Override
public void onFinish() {
myCallback.callback();
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
public void setMyCallback(MyCallback myCallback){
this.myCallback = myCallback;
}
}
Then, in each of your activities, you would implement the new interface like so:
public class Main extends Activity implements MyCountDownTimer.MyCallback
{
private MyCountDownTimer myCountDownTimer;
#Override
public void callback(){
Log.e("Testing", "OK na");
}
#Override
public void onCreate(Bundle savedInstanceState){
myCountDownTimer = new MyCountDownTimer(5000, 1000);
myCountDownTimer.setMyCallback(this); //will use the callback method in this class which can be different for each activity
myCountDownTimer.start();
}
}
CountDownTimer is a very simple class, you don't need to make an entire class for it, you could create it on your caller class and in that way you could call the callback function on finish.
public class MyActivity extends Activity{
//Other methods and variables
CountDownTimer countdowntimer = new CountDownTimer(5000,1000){
#Override
public void onFinish() {
//Call the callback from your activity
}
#Override
public void onTick(long millisUntilFinished) {
Log.e("TESTINg","" + millisUntilFinished/1000);
}
};
}
If for some reason you must to make it in a different class then in that class you must create an interface that your activity must implement and in your finish method call the method of the interface listener.
Like in fragments: https://stackoverflow.com/a/34192088/2367237
I don't think you can return a value from CountDownTimer.
Ideally you should start the timer and implement what you have to do in the call back methods -
abstract void onFinish()
//Callback fired when the time is up.
abstract void onTick(long millisUntilFinished)
//Callback fired on regular interval.
sample implementation
https://androidcookbook.com/Recipe.seam;jsessionid=DF53064E03C7505C4EBF727E56E0728E?recipeId=1205
I think it's not working because you never set the variable finish to true on your code. You have to change your code in onFinish method to finish = true then put a logic inside onTick method to check if it's already finished then call onFinish() method;

If I go out of activities where there is a timer turns it off

When I go out from activity where is timer the timer is still going. How to make himself stop after leaving the activity?
This is my code:
new CountDownTimer(15000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
textView.setText(" " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
Intent myIntent=new Intent(hra1.this, timeout.class);
startActivity(myIntent);
finish();
}
}
.start();
}
declare your countdown timer before your onCreate method
CountDownTimer myTimer;
Where you are making new CountdownTimer, do it like this
myTimer = new CountDownTimer(15000, 1000) // etc
write these lines in your onPause method
myTimer.cancel();
myTimer = null;
try
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
public void onTick(long millisUntilFinished) {
textView.setText(" " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
Intent myIntent=new Intent(hra1.this, timeout.class);
startActivity(myIntent);
finish();
}
}
create timer object
CountDownTimer countDownTimer = new MyCountDownTimer(15000, 1000);
to start timer
countDownTimer.start();
to stop timer
countDownTimer.cancel();
stop timer at onPause method

Categories