java - Set a countdown till 4 hours after current time[App not working] -
i want timer runs 4 hours current time, , till wrong_answer bit set 0 again, should not stop in between if application closes.
i tried variations morning , still app crashes when reach activity.
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 on :)"); 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; } }
i know not efficient way still want know solution.
i feel way i'm parsing future date.
i have copy pasted code app working fine ,just setting fixed date other there :
simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm"); date futuredate = dateformat.parse("2017-09-12 1:21"); date currentdate = new date();
instead of retrieving savedpreference method .
will saving long variable help??
thanks
this see on logcat logcat:
09-12 19:05:54.111 23653-23653/? i/fa: app measurement starting up, version: 10298
09-12 19:05:54.111 23653-23653/? i/fa: enable debug logging run: adb shell setprop log.tag.fa verbose
09-12 19:05:54.119 23653-23653/? i/fa: enable faster debug mode event logging run: adb shell setprop debug.firebase.analytics.app com.bitstrips.imoji
nevermind solved .
had use:
future_date = dateformat.format("yyyy-mm-dd hh:mm:ss", new date(nowplus4hours)).tostring();
instead of :
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
:)
Comments
Post a Comment