Reputation: 142
I have an app which has 2 activity, so I want to set the timer when the timer finishes it's gone next activity. I was trying for 5 seconds and it's work. But I face some problem when I am trying to time in days, hours, seconds but I can't. Suppose I set this time 20.10.2019,10:00 AM when this set time finish, it's going to next activity.
How to do this? Have any code, resource or project where I learn this thing?
Upvotes: 0
Views: 129
Reputation: 142
UPDATE, I tried and it works.
public class timer extends AppCompatActivity {
/*Views declaration*/
private TextView months_left,weeks_left,daysLeft,hrsLeft,minLeft,secLeft,endDate;
/*Handler Declaration*/
private Handler handler;
/*set End Time for timer */
private String endDateTime="2019-07-13 06:22:30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
initView();
}
private void initView() {
months_left = findViewById(R.id.txtViewDays);
weeks_left = findViewById(R.id.txtViewHours);
daysLeft = findViewById(R.id.txtViewMinutes);
hrsLeft = findViewById(R.id.txtViewSecond);
minLeft = findViewById(R.id.misleft);
secLeft = findViewById(R.id.seclft);
endDate = findViewById(R.id.enddate);
endDate.setText(endDateTime);
/*invoke countDownStart() method for start count down*/
countDownStart();
}
private void countDownStart() {
handler = new Handler();
Runnable runnable = new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// Please set date in YYYY-MM-DD hh:mm:ss format
/*parse endDateTime in future date*/
Date futureDate = dateFormat.parse(endDateTime);
Date currentDate = new Date();
/*if current date is not comes after future date*/
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days *(24 *60 * 60 *1000);
long hours = diff / (60 * 60* 1000);
diff -= hours * (60* 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 *1000);
long seconds = diff / 1000;
@SuppressLint("DefaultLocale") String dayLeft = "" + String.format("%02d", days);
@SuppressLint("DefaultLocale") String hrLeft = "" + String.format("%02d", hours);
@SuppressLint("DefaultLocale") String minsLeft = "" + String.format("%02d", minutes);
@SuppressLint("DefaultLocale") String secondLeft = "" + String.format("%02d", seconds);
daysLeft.setText(dayLeft + "D: ");
hrsLeft.setText(hrLeft + "H: ");
minLeft.setText(minsLeft + "M: ");
secLeft.setText(secondLeft + "S");
if (days <= 0) {
if (hours <= 0) {
if (minutes <= 0) {
if (seconds <= 0) {
// put your intent here
myActivity();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1000);
}
private void myActivity() {
Intent myActivity= new Intent(timer.this, New.class);
startActivity(myActivity);
}
}
Upvotes: 1
Reputation: 7196
You can use a Handler
to start the next activity after some time, like this
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
},3000);
this will start the second activity after 3000 ms or 3 seconds
Upvotes: 0