【问题标题】:Set a countdown till 4 hours after current time[App not working]设置倒计时到当前时间后 4 小时[应用程序不工作]
【发布时间】:2017-09-12 13:20:26
【问题描述】:

我想要一个从当前时间开始运行 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 上看到的全部内容 日志猫:

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

【问题讨论】:

  • 是否需要每秒更新一次?
  • 是的,正在屏幕上显示
  • 然后使用CountDownTimer
  • 应用程序崩溃?发布您的 LogCat
  • CountDownTimer 即使应用程序关闭也不会运行,对吗?这将检查剩余时间并根据它设置时间。

标签: java android


【解决方案1】:

没关系,我解决了。

必须使用:

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

而不是:

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

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2018-12-31
    • 2021-01-23
    相关资源
    最近更新 更多