我已经尝试了很多,但我的textview没有改变。
计时器应该自动添加30分钟到当前时间,这将是我未来的时间事件按钮。单击
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAdding30Mins=new Date(t + (30 * ONE_MINUTE_IN_MILLIS));
private void countDownStart() {
runnable = new Runnable() {
@Override
public void run() {
try {
handler.postDelayed(this, 1000);
if (!current_date.after(afterAdding30Mins)) {
long diff = t+ 1800000;
long minutes = (diff / 1000) / 60;
long seconds = (diff / 1000) % 60;
String timeLeftFormattedm = String.format(Locale.getDefault(), "%02d", minutes);
String timeLeftFormatteds = String.format(Locale.getDefault(), "%02d", seconds);
//
tv_minute.setText(timeLeftFormattedm);
tv_second.setText(timeLeftFormatteds);
} else {
linear_layout_1.setVisibility(View.VISIBLE);
linear_layout_2.setVisibility(View.GONE);
handler.removeCallbacks(runnable);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}
protected void onStop() {
super.onStop();
handler.removeCallbacks(runnable);
}
}
答案 0 :(得分:0)
您需要的是 BroadcastReceiver 。使用BroadcastReceiver,您可以为将来安排任务或事件,即使应用程序被销毁/关闭,它们仍然可以运行/工作。在您的情况下,您可以在按下按钮后立即呼叫BroadcastReceiver并安排30分钟。
了解BroadcastReceivers的工作原理:通读this guide或查看 this question。
这个YouTube video 也会给你一个开端,