java - How to read Sharedpreference value from another activity -


i make 2 activity first timer.java activity countdown activity , second activity saveresttime.java in activity user input number value in edittext , user save use shared preference in activity want value user save in saveresttime.java class automatically make value of countdown if user not save value default value 30 sec please me confuse how read value activity here timer.java code

package com.cinegoes.www.daily10exercise;  import android.app.activity; import android.content.context; import android.content.sharedpreferences; import android.os.bundle; import android.os.countdowntimer; import com.cinegoes.www.daily10exercise.saveresttime; import android.view.view; import android.widget.button; import android.widget.progressbar; import android.widget.textview;  import java.util.concurrent.timeunit;  import static com.cinegoes.www.daily10exercise.saveresttime.mypreference;  /**  * created ever on 7/25/2017.  */  public class timer extends activity implements view.onclicklistener {  private countdowntimer countdowntimer; private boolean timerstarted = false; private button buttonstart; public textview textview; private progressbar progressbarcircle; private final long starttime = 30 * 1000; private final long interval = 1 * 1000;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.timer);     buttonstart = (button) this.findviewbyid(r.id.button);     progressbarcircle = (progressbar) findviewbyid(r.id.progressbarcircle);     buttonstart.setonclicklistener(this);     textview = (textview) this.findviewbyid(r.id.textview);     countdowntimer = new countdowntimeractivity(starttime, interval);     textview.settext(textview.gettext() + string.valueof(starttime/1000)); }  @override protected void onstart() {     super.onstart();     countdowntimer.start();     timerstarted = true;     setprogressbarvalues(); }   @override public void onclick(view v) {     finish(); }   public class countdowntimeractivity extends countdowntimer {     public countdowntimeractivity(long starttime, long interval) {         super(starttime, interval);     }      @override     public void onfinish() {         textview.settext(mstimeformatter(starttime));         textview.settext("time's up!");         setprogressbarvalues();         finish();     }       @override     public void ontick(long millisuntilfinished) {         textview.settext(mstimeformatter(millisuntilfinished));          progressbarcircle.setprogress((int) (millisuntilfinished / 1000));     } }  private void setprogressbarvalues() {      progressbarcircle.setmax((int) starttime / 1000);     progressbarcircle.setprogress((int) starttime / 1000); } private string mstimeformatter(long milliseconds) {      string ms = string.format("%02d:%02d",             timeunit.milliseconds.tominutes(milliseconds) -  timeunit.hours.tominutes(timeunit.milliseconds.tohours(milliseconds)),             timeunit.milliseconds.toseconds(milliseconds) -  timeunit.minutes.toseconds(timeunit.milliseconds.tominutes(milliseconds)));      return ms;   }   } 

here saveresttime.java code

package com.cinegoes.www.daily10exercise;  import android.app.activity; import android.content.context; import android.content.sharedpreferences; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.textview;  public class saveresttime extends activity { sharedpreferences sharedpreferences; textview name; public static final string mypreference = "mypref"; public static final string name = "namekey";   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.saveresttime);      name = (textview) findviewbyid(r.id.etname);     sharedpreferences = getsharedpreferences(mypreference,             context.mode_private);     if (sharedpreferences.contains(name)) {         name.settext(sharedpreferences.getstring(name, ""));     }  }  public void save(view view) {     string n = name.gettext().tostring();     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putstring(name, n);     editor.commit(); }    public void get(view view) {     name = (textview) findviewbyid(r.id.etname);     sharedpreferences = getsharedpreferences(mypreference,  context.mode_private);      if (sharedpreferences.contains(name))  {name.settext(sharedpreferences.getstring(name, ""));     }  }  } 

sharedpreferences prefs = this.getsharedpreferences("mypref", context.mode_private); string lansettings = prefs.getstring("namekey", null); 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -