3

As we know to save something like int value's we use this

   SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
   SharedPreferences.Editor editor = pref.edit();
   editor.putInt("Something", "Value");
   editor.commit();
        }

now my question how to save something like chronometer time with sharedpreferences? as we know chronometer code like this

    focus = (Chronometer) findViewById(R.id.chronometer1);      
    focus.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
            public void onChronometerTick(Chronometer cArg) {
            long time = SystemClock.elapsedRealtime() - cArg.getBase();
            int h   = (int)(time /3600000);
            int m = (int)(time - h*3600000)/60000;
            int s= (int)(time - h*3600000- m*60000)/1000 ;
            String hh = h < 10 ? "0"+h: h+"";
            String mm = m < 10 ? "0"+m: m+"";
            String ss = s < 10 ? "0"+s: s+"";
            cArg.setText(hh+":"+mm+":"+ss);
        }
    }); 

to start the chronometer, put this in a button called "Button Start"

               focus.setBase(SystemClock.elapsedRealtime());
               focus.start();

to stop the chronometer, put this in a button called "Button Stop"

              focus.stop();

Ok, so my question is how to save the chronometer time/tick if "Button Stop" was pressed? Thank's

Ricci
  • 217
  • 5
  • 21

1 Answers1

0

Using your terminology, you have to put this piece of code:

   SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
   SharedPreferences.Editor editor = pref.edit();
   editor.putInt("Something", "Value");
   editor.commit();

into your 'Button Stop' OnClickListener

stopButton.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {
  //here
  }
});
agamov
  • 4,407
  • 1
  • 27
  • 31