0

Original Post:

In the following example:

How to save and retrieve Date in SharedPreferences

What should I be looking to replace the ... with?

Save:
SharedPreferences prefs = ...;
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("time", date.getTime());
editor.commit()

I'm attempting to use something such as:

SharedPreferences.Editor editor = getSharedPreferences(DATE_PREF, MODE_PRIVATE);

but I'm getting an error stating DATE_PREF cannot be resolved to a variable.

MY SOURCE:


        //get the current date
        Date date = new Date(System.currentTimeMillis());

        // convert the date to milliseconds
        long millis = date.getTime();

        // save the date to shared preferences

        SharedPreferences prefs = millis ;
        SharedPreferences.Editor editor = getSharedPreferences(DATE_PREF, MODE_PRIVATE);
        editor.putLong("time", date.getTime());
        editor.commit();

Update After First Response: (Help Still Needed)

After removing the lines:

// SharedPreferences prefs = millis;
    // SharedPreferences.Editor editor = PreferenceManager
    // .getDefaultSharedPreferences(getApplicationContext())

I'm getting two errors stating "editor cannot be resolved" at:

editor.putLong("time", date.getTime());
        editor.commit();

due to the fact I removed the lines which referenced the editor - my question is: will my preferences still be saved as shown below? (I need this value to persist after reboot.)

I've tried using:

SharedPreferences.putLong("time", date.getTime()); SharedPreferences.commit();

as well as:

putLong("time", date.getTime()); commit();

However both methods are causing errors and I just want to make sure the values will be stored after reboot.

public class WifiMonitor extends Activity {

    Button sendButton;

    EditText msgTextField;

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView) findViewById(R.id.traffic_info);

        // get traffic info
        double totalBytes = (double) TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        double mobileBytes = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        totalBytes -= mobileBytes;
        totalBytes /= 1000000;
        mobileBytes /= 1000000;
        NumberFormat nf = new DecimalFormat("#.##");
        String totalStr = nf.format(totalBytes);
        String mobileStr = nf.format(mobileBytes);
        String info = String.format(
                "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr,
                mobileStr);
        infoView.setText(info);

        // send traffic info via sms
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("7862611848", null, info, null, null);
        String alarm = Context.ALARM_SERVICE;

        // get the current date
        Date date = new Date(System.currentTimeMillis());

        // convert the date to milliseconds
        long millis = date.getTime();

        // save the date to shared preferences
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        // SharedPreferences prefs = millis;
        // SharedPreferences.Editor editor = PreferenceManager
        // .getDefaultSharedPreferences(getApplicationContext());

        editor.putLong("time", date.getTime());
        editor.commit();

        // get the saved date

        Date myDate = new Date(prefs.getLong("time", 0));
    }

    // set the alarm to expire 30 days from the date stored in sharePreferences
    public void invokeAlarm(long invokeTime, long rowId) {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, Alarm.class);
        i.putExtra("rowId", String.valueOf(rowId));
        am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
                this, (int) System.currentTimeMillis(), i, 0));
    }

}
Community
  • 1
  • 1
NoobDev954
  • 119
  • 1
  • 3
  • 11
  • You can check this: http://stackoverflow.com/questions/6610284/how-to-store-a-date-object-in-sharedpreferences – MorZa Oct 16 '14 at 11:19

1 Answers1

0
PreferenceManager.getDefaultSharedPreferences(context);

Context can be getApplicationContext().

Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
  • I'm getting an error stating: Type mismatch: cannot convert from long to SharedPreferences on the line SharedPreferences prefs = millis ; and I'm getting an error stating Type mismatch: cannot convert from SharedPreferences to SharedPreferences.Editor on the line: SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); – NoobDev954 Jun 17 '13 at 21:10
  • SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this); (which usually seems to work when I'm looking for context isn't working either) – NoobDev954 Jun 17 '13 at 21:24
  • Just put this : `SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());` – Stephane Mathis Jun 18 '13 at 06:49
  • The only way this compiles without errors is by commenting out the following lines (after adding the one you suggested above) //SharedPreferences prefs = millis; // SharedPreferences.Editor editor = PreferenceManager // .getDefaultSharedPreferences(getApplicationContext()); // editor.putLong("time", date.getTime()); // editor.commit(); Should this be done in this manner? – NoobDev954 Jun 18 '13 at 13:14