繁体   English   中英

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

[英]How To Retrieve And Save a Date Via Shared Preferences

原始帖子:

在以下示例中:

如何在SharedPreferences中保存和检索Date

我应该如何替换...?

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

}
PreferenceManager.getDefaultSharedPreferences(context);

上下文可以是getApplicationContext()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM