0

I have a Chronometer app that I want to maintain its time when the device is rebooted. For example if its at 10 minutes on reboot I want it to continue from 10 minutes when the device reboots.

I am attempting to save the value of the Chronometer in shared preferences.

I believe I can get the value of the chronometer by using the below code but I am unsure how to store it in SavedPreferences and recall it once the app opens again. Any Help is appreciated.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    chronometer = (Chronometer) findViewById(R.id.chronometer1);

    long chronostore = chronometer.getBase();
Will
  • 11,276
  • 9
  • 68
  • 76
steelthunder
  • 438
  • 2
  • 12
  • 27

1 Answers1

0

To use shared Prefs, you need to save the value to SharedPreferences:

// Update your preferences when the chronometer is paused, so put this in your OnPause
SharedPreferences prefs = getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
// Don't forget to call commit() when changing preferences.
prefs.edit().putLong("chronoValue", chrono.getValue()).commit();        

//In your OnResume, you need to call the value back
SharedPreferences prefs = getSharedPreferences("mySharedPrefsFilename",            Context.MODE_PRIVATE);
chornostore = prefs.getLong("chronoValue", 0); // 0 is default

That should get you on the right track.


Edit:

So the deal with SharedPrefs is that you can store and access data from anywhere in your application - the catch is that the data has to be Float, String, Int, Boolean, Long or a String array (set). Since you're trying to save a Long value from the Chronometer, you need to save that value via putLong

Getting the Data:

Step 1: initialize a SharedPreferences instance using getSharedPreferences:

// Context.MODE_PRIVATE means only this application has access to the files. 
// You can set any name for the file, and the instance. I used "SharedPrefsFileName" and prefs respectively.
SharedPreferences prefs = getSharedPreferences("SharedPrefsFileName", Context.MODE_PRIVATE);

Step 2: Call the data from your SharedPreferences instance:

// The second argument is the value to return if there is no "chronoValue" data. In this case, it will return "0"
chornostore = prefs.getLong("chronoValue", 0); 

Storing the Data:

Step 1: Initialize a SharedPreferences.Editor

//Use the same SharedPreferences instance and then instantiate an editor for storing:
SharedPreferences prefs = getSharedPreferences("SharedPrefsFileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

Step 2: Store the Data:

//Use the same "chronoValue" Name for the Name-Value pair that is accepted as the arguments:
editor.putLong("chronoValue", chronostore);

Step 3: Commit the changes to SharedPreferences:

editor.commit();

And that's all there is to it.

Depending on your usage, you might find it helpful to store/call the values in your application's onPause and onResume methods.

If you need more help, read through the helpful usage guide here:

http://developer.android.com/guide/topics/data/data-storage.html#pref

or the documentation here:

http://developer.android.com/reference/android/content/SharedPreferences.html

SQLiteNoob
  • 2,958
  • 3
  • 29
  • 44
  • Thanks for your help. I havent been able to get it to work. If you could elaborate more. Am I storing the chronometer as a a variable which is in turn stored in SavedPreferences? I am not familiar with SavedPrefrences so if you could explain it to me I would appreciate it. – steelthunder Mar 18 '14 at 10:57
  • Basically, SharedPreferences stores name-value pairs. You can store your chronometer data under any name as long as you call it from the same name. The value is what you're storing - the data, the variable of it, not an instance of the object - but the value can represent a reference to an instance if that helps. – SQLiteNoob Mar 19 '14 at 03:32