Reputation: 142
I have two activity set countdown timer in MainActivity, I want when countdown timer finished it will go next activity.but it's not going to next activity its show timer. here is my code:
public class MainActivity extends AppCompatActivity {
TextView txtViewDays,txtViewHours,txtViewMinutes,txtViewSecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtViewDays = findViewById(R.id.days);
txtViewHours = findViewById(R.id.hours);
txtViewMinutes = findViewById(R.id.minutes);
txtViewSecond = findViewById(R.id.seconds);
start_countdown_timer();
}
private void start_countdown_timer()
{
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
final long[] startTime = new long[1];
String endTime = "23.06.2019, 22:56:10";
long milliseconds=0;
final CountDownTimer mCountDownTimer;
Date endDate;
try {
endDate = formatter.parse(endTime);
milliseconds = endDate.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime[0] = System.currentTimeMillis();
mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Long serverUptimeSeconds =
(millisUntilFinished - startTime[0]) / 1000;
String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
txtViewDays.setText(daysLeft);
String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
txtViewHours.setText(hoursLeft);
String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
txtViewMinutes.setText(minutesLeft);
Log.d("minutesLeft",minutesLeft);
String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
txtViewSecond.setText(secondsLeft);
}
@Override
public void onFinish() {
Intent i = new Intent(MainActivity.this,New.class);
startActivity(i);
finish();
}
};
mCountDownTimer.start();
}
}
What can I do? N.B : code is collected and I also try but I can't.Thank you
Upvotes: 0
Views: 168
Reputation: 739
With your code you need 1561330519961ms that means several days must pass in order for onFinish method to be called. I tried your code with 1min for the timer instead
milliseconds = endDate.getTime();
and worked just fine. After the timer finished it changed activity.
---edit---
This sample code will run for 6 seconds and then call the onFinish method
milliseconds = 6000
mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
Intent i = new Intent(MainActivity.this,Myclass.class);
startActivity(i);
finish();
}
};
mCountDownTimer.start();
Upvotes: 1