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.
Related
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();
...
}
});
}
}
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
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
I need to return the value from my countdowntimer so I can update a textView. I'm not sure how I can do this, I've added a comment where I've tried adding a return but no luck.
public class BrewTimer extends Activity{
public String secsRemain = "not set";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Timer Created, we're in the onCreate Method.");
}
public void startBrewTimer(long remaningMillis) {
// Start the Brew Timer
System.out.println("Timer object fired!");
new CountDownTimer(remaningMillis, 1000) {
public void onTick(long millisUntilFinished) {
secsRemain = "seconds remaining: " + millisUntilFinished / 1000;
System.out.println(secsRemain);
return secsRemain; // <-- This is what I want to do
}
public void onFinish() {
System.out.println("Timer Finished!!!");
}
}.start();
}
public void stopBrewTimer() {
//Stop the Brew Timer
}
}
I need to return the value from my countdowntimer so I can update a
textView.
There is no need to do that. It runs on the ui thread. You can update textview in onTick itself.
Example:
public class MainActivity extends Activity {
TextView tv;
public String secsRemain = "not set";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
startBrewTimer(200000);
}
private void startBrewTimer(long remaningMillis){
CountDownTimer counter = CountDownTimer(remaningMillis, 1000){
public void onTick(long millisUntilDone){
secsRemain = "seconds remaining: " + millisUntilFinished / 1000;
tv.setText(secsRemain);
}
public void onFinish() {
tv.setText("DONE!");
}
}.start();
}
}
You can get values through broadcast receiver......as follows,
First create your own IntentFilter as,
Intent intentFilter=new IntentFilter();
intentFilter.addAction("YOUR_INTENT_FILTER");
Then create inner class BroadcastReceiver as,
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
/** Receives the broadcast that has been fired */
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction()=="YOUR_INTENT_FILTER"){
//HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
String receivedValue=intent.getStringExtra("KEY");
}
}
};
Now Register your Broadcast receiver in onResume() as,
registerReceiver(broadcastReceiver, intentFilter);
And finally Unregister BroadcastReceiver in onDestroy() as,
unregisterReceiver(broadcastReceiver);
Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,
Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);
don't forget to accept my answer if you find this ans satisfactory....cheers :)
here is my code.but if this runs my app closed from 60seconds.but when i again press app icon.its open as logged.how to close app with logout.?i added these in my service class.
public class BackgroundService extends Service {
private int interval3 = 10; // 10 seconds
private Handler mTimer3 = new Handler();
private Runnable mTask3 = new Runnable() {
public void run() {
mTimer3.postDelayed(this, interval3 * 1000L);
CountDownTimer timer = new CountDownTimer(10*1000, 1000) {
public void onTick(long millisUntilFinished) {
Log.w("Seconds remaining: ", String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
timer.start();
}
};
public void onCreate() {
mTimer1.postDelayed(mTask1, interval1 * 1000L); // start the timer for the first time
mTimer2.postDelayed(mTask2, interval2 * 1000L); // start the timer for the first time
mTimer3.postDelayed(mTask3, interval3 * 1000L); // start the timer for the first time
}
please give me a suggestion.because we need to sign out from app when idle at more time.
thanks all
now its ok.i tried with activity class instead of service class and i added code to after login screen going page.now its working perrfect for me :-)
public class #MyActivityClass extends Activity {
//=============================================================================================================================
private Handler mTimer3 = new Handler();
private Runnable mTask3 = new Runnable() {
public void run() {
CountDownTimer timer = new CountDownTimer(10*1000, 1000) {
public void onTick(long millisUntilFinished) {
Log.w("Seconds remaining: ", String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
//Here finally added finish(); and System.exit(0); methods
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);
}
};
timer.start();
mTimer3.postDelayed(this, interval3 * 1000L);
}
};
private int interval3 = 10*60; // 60 seconds
#Override
protected void onDestroy() {
if (mTimer3 != null) {
mTimer3.removeCallbacks(mTask3); // cancel the timer
}
}