我希望计时器从当前时间开始运行4个小时,直到将rong_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();


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


  将其保存为长变量会有所帮助吗?


谢谢

这就是我在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:要启用更快的调试模式事件记录,请运行:
                                           亚行外壳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");


:)

10-04 11:27