現在の時刻から4時間実行するタイマーを設定し、wrong_answerビットが再び0に設定されるまでは、アプリケーションは終了します。現在時刻の4時間後までのカウントダウンを設定する[アプリが動作しない]
私はこのアクティビティに達すると、午前中にバリエーションを試しても、アプリがクラッシュします。
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;
}
}
私はその最も効率的な方法ではありませんが、私はまだ解を知りたいと思います。
私は将来の日付を解析している方法と関係があります。 savedPreferenceメソッドからそれを取得するのではなく、
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date futureDate = dateFormat.parse("2017-09-12 1:21");
Date currentDate = new Date();
:
私はちょうど私がようにそこ他の固定の日付を設定して、正常に動作している私の他のアプリから、このコードを貼り付けコピーしています。
長い可変ヘルプとして保存しますか?
おかげ
これはlogcat Logcatに私が見るすべてである:
9月12日19:05:54.111 23653から23653 /? I/FA:アプリ測定開始、バージョン:10298
09-12 19:05:54.111 23653-23653 /? I/FA:デバッグログを有効にするには:adbシェルsetprop log.tag.FA VERBOSE
09-12 19:05:54.119 23653-23653 /? I/FAは: adbのシェルに、SetProp debug.firebase.analytics.appのcom.bitstrips.imoji
それは毎秒更新取得するために必要ですか? –
はい、それが画面に表示されています –
次にCountDownTimerを使用して –