将倒计时设置为当前时间后4小时

我想要一个从当前时间运行4个小时的计时器,直到wrong_answer位再次设置为0,即使应用程序关闭,也不应该停止。

我整个上午尝试过各种变体,当我到达此活动时仍然会崩溃。

public class activityWrong extends AppCompatActivity { CountdownView countview; TextView tv2,txtTimerHour,txtTimerMinute,txtTimerSecond ; Button tryagain; Calendar future; Date future_date; private Handler handler; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wrong); txtTimerHour = (TextView) findViewById(R.id.txtTimerHour); txtTimerMinute = (TextView) findViewById(R.id.txtTimerMinute); txtTimerSecond = (TextView) findViewById(R.id.txtTimerSecond); //countview = (CountdownView) findViewById(R.id.countdownView); if(get_wrong_bit() == 0) { Calendar cl = Calendar.getInstance(); long nowPlus4hours = cl.getTimeInMillis() + 14400000; Toast.makeText(activityWrong.this, String.valueOf(nowPlus4hours) , Toast.LENGTH_SHORT).show(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Toast.makeText(activityWrong.this, formatter.format(future.getTime()) , Toast.LENGTH_SHORT).show(); store_countdown(formatter.format(future.getTime())); set_wrong_bit(1); } countDownStart(); tryagain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { set_wrong_bit(0); Intent stud = new Intent(activityWrong.this,activityQuiz.class); startActivity(stud); finish(); } }); } public void countDownStart() { tv2 = (TextView) findViewById(R.id.tv2); tryagain = (Button) findViewById(R.id.try_again); handler = new Handler(); runnable = new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date futureDate = dateFormat.parse(get_countdown()); Date currentDate = new Date(); if (!currentDate.after(futureDate)) { long diff = futureDate.getTime() - currentDate.getTime(); long hours = diff / (60 * 60 * 1000); diff -= hours * (60 * 60 * 1000); long minutes = diff / (60 * 1000); diff -= minutes * (60 * 1000); long seconds = diff / 1000; txtTimerHour.setText("" + String.format("%02d", hours)); txtTimerMinute.setText("" + String.format("%02d", minutes)); txtTimerSecond.setText("" + String.format("%02d", seconds)); } else { tv2.setText("Punishment Over :)"); tryagain.setVisibility(View.VISIBLE); } } catch (Exception e) { e.printStackTrace(); } } }; handler.postDelayed(runnable, 1 * 1000); } private void set_wrong_bit(int wrong_bit) { SharedPreferences mSharedPreferences = getSharedPreferences("main",MODE_PRIVATE); SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putInt("wrong_bit",wrong_bit); mEditor.apply(); } private int get_wrong_bit(){ SharedPreferences mSharedPreferences = getSharedPreferences("main",MODE_PRIVATE); int checker = mSharedPreferences.getInt("wrong_bit",0); return checker; } private void store_countdown(String countdown) { SharedPreferences mSharedPreferences = getSharedPreferences("main",MODE_PRIVATE); SharedPreferences.Editor mEditor = mSharedPreferences.edit(); mEditor.putString("countdown",countdown); mEditor.apply(); } private String get_countdown(){ SharedPreferences mSharedPreferences = getSharedPreferences("main",MODE_PRIVATE); String checker = mSharedPreferences.getString("countdown","2017-09-21 17:20:00"); return checker; } } 

我知道它不是最有效的方式,但我仍然想知道解决方案。

我觉得这与我解析未来日期的方式有关。

我复制粘贴这个代码从我的另一个应用程序工作正常,只是我设置一个固定的日期,其他像:

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm"); Date futureDate = dateFormat.parse("2017-09-12 1:21"); Date currentDate = new Date(); 

而不是从savedPreference方法中检索它。

将它保存为长变量有帮助吗?

谢谢

这是我在logcat Logcat上看到的全部内容:

09-12 19:05:54.111 23653-23653 /? I / FA:应用程序测量正在启动,版本:10298

09-12 19:05:54.111 23653-23653 /? I / FA:启用调试日志记录运行:adb shell setprop log.tag.FA VERBOSE

09-12 19:05:54.119 23653-23653 /? I / FA:要启用更快的调试模式事件日志记录运行:adb shell setprop debug.firebase.analytics.app com.bitstrips.imoji

没关系,我解决了它。

不得不使用:

 future_date = DateFormat.format("yyyy-MM-dd HH:mm:ss", new Date(nowPlus4hours)).toString(); 

代替 :

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

🙂