如何通过共享首选项检索并保存日期

时间:2013-06-17 21:04:16

标签: java android sharedpreferences alarm reboot

原帖:

在以下示例中:

How to save and retrieve Date in SharedPreferences

我应该用什么来替换...?

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

我正在尝试使用以下内容:

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

但是我收到一条错误,指出DATE_PREF无法解析为变量。

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();

第一次回应后更新:(仍然需要帮助)

删除行后:

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

我收到两条错误,指出“编辑器无法解决”:

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

由于我删除了引用编辑器的行 - 我的问题是:我的首选项是否仍会保存如下所示? (重启后我需要保留此值。)

我尝试过使用:

SharedPreferences.putLong(“time”,date.getTime());         SharedPreferences.commit();

以及:

putLong(“time”,date.getTime());         提交();

但是这两种方法都会导致错误,我只想确保重启后存储这些值。

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));
    }

}

1 个答案:

答案 0 :(得分:0)

PreferenceManager.getDefaultSharedPreferences(context);

上下文可以是getApplicationContext()。