java - My step counter does not reset the steps, even after uninstall -


i use sensor.type_step_counter know resets when rebooting. there alternative way reset steps 0 when pressing button?

please see code, find runactivity.class

maybe can in way resets steps. without having me reboot every time.

public class runactivity extends appcompatactivity implements sensoreventlistener{      private sensormanager sensormanager;     private textview count;     boolean activityrunning;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_run);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);          bundle bundle = getintent().getextras();         final string naam   = bundle.getstring("naam");         textview naamview = null;          button stoprun = (button) findviewbyid(r.id.stoprun);          count = (textview) findviewbyid(r.id.countview);         sensormanager = (sensormanager) getsystemservice(context.sensor_service);          naamview =  (textview) findviewbyid(r.id.naamruntext);          naamview.settext(naam);          stoprun.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  string countvalue = count.gettext().tostring();                    log.d("countval", string.valueof(countvalue));                   intent myintent = new intent(runactivity.this, homescreenactivity.class);                  bundle bundle = new bundle();                   bundle.putstring("naam", naam);                 sensormanager.flush(runactivity.this);                 sensormanager.unregisterlistener(runactivity.this);                 count.settext("0");                 onstop();                  myintent.putextras(bundle);                  startactivity(myintent);               }         });      }        @override     protected void onresume() {         super.onresume();         activityrunning = true;         sensor countsensor = sensormanager.getdefaultsensor(sensor.type_step_counter);         if(countsensor != null){             sensormanager.registerlistener(this, countsensor, sensormanager.sensor_delay_ui);          }else{             toast.maketext(this, "jouw apparaat heeft geen sensor!", toast.length_long) .show();         }     }      @override     public void onsensorchanged(sensorevent event) {         if(activityrunning){             count.settext(string.valueof(event.values[0]));         }else{             event.values[0] = 0;         }        }        @override     protected void onpause() {         super.onpause();         sensormanager.unregisterlistener(this);         activityrunning = false;     }        @override     public void onaccuracychanged(sensor sensor, int accuracy) {      }      protected void ondestroy() {         super.ondestroy();      } } 

when click reset button in app save current step count sharedpreferences. , you'll need way find out when last reboot because every time reboot saved count number gets invalid.

private int stepsinsensor = 0; private int stepsatreset;  void oncreate() {   sharedpreferences prefs = getsharedpreferences(my_prefs_name, mode_private);    stepsatreset = prefs.getint("stepsatreset", 0); }      public void onclick(view v) {         stepsatreset = stepsinsensor;          sharedpreferences.editor editor =             getsharedpreferences(my_prefs_name, mode_private).edit();         editor.putint("stepsatreset", stepsatreset);         editor.commit();          // can display 0:         count.settext(string.valueof(0));     }  @override public void onsensorchanged(sensorevent event) {     if(activityrunning){         stepsinsensor = integer.valueof(event.values[0]);         int stepssincereset = stepsinsensor - stepsatreset;         count.settext(string.valueof(stepssincereset));     }else{         event.values[0] = 0;     } } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -