Android 迄今为止和下一个 activity 的倒数计时器?
Android Countdown timer to date and next activity?
我有一个有 2 个 activity 的应用程序,所以我想在定时器结束时设置定时器,下一个 activity。我尝试了 5 秒钟,它成功了。但是当我尝试以天、小时、秒为单位计时但我做不到时,我遇到了一些问题。
假设我设置这个时间 20.10.2019,10:00 AM 当这个设置时间结束时,它将进入下一个 activity.
如何做到这一点?有任何代码、资源或项目让我学习这件事吗?
你可以用一个Handler
在一段时间后开始下一个activity,像这样
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
},3000);
这将在 3000 毫秒或 3 秒后开始第二个 activity
更新,我试过了并且有效。
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);
}
}
我有一个有 2 个 activity 的应用程序,所以我想在定时器结束时设置定时器,下一个 activity。我尝试了 5 秒钟,它成功了。但是当我尝试以天、小时、秒为单位计时但我做不到时,我遇到了一些问题。 假设我设置这个时间 20.10.2019,10:00 AM 当这个设置时间结束时,它将进入下一个 activity.
如何做到这一点?有任何代码、资源或项目让我学习这件事吗?
你可以用一个Handler
在一段时间后开始下一个activity,像这样
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
},3000);
这将在 3000 毫秒或 3 秒后开始第二个 activity
更新,我试过了并且有效。
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);
}
}