【问题标题】:How To Retrieve And Save a Date Via Shared Preferences如何通过共享首选项检索和保存日期
【发布时间】:2013-06-17 21:04:15
【问题描述】:

原帖:

在以下示例中:

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("时间", date.getTime()); SharedPreferences.commit();

还有:

putLong("时间", 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));
    }

}

【问题讨论】:

标签: java android sharedpreferences alarm reboot


【解决方案1】:
PreferenceManager.getDefaultSharedPreferences(context);

上下文可以是getApplicationContext()。

【讨论】:

  • 我收到一条错误消息:Type mismatch: cannot convert from long to SharedPreferences on the line SharedPreferences prefs = millis ;并且我收到一条错误消息,指出 Type mismatch: cannot convert from SharedPreferences to SharedPreferences.Editor on the line: SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  • SharedPreferences.Editor 编辑器 = PreferenceManager.getDefaultSharedPreferences(this); (当我寻找上下文时,这通常似乎也不起作用)
  • 放这个:SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  • 唯一没有错误编译的方法是注释掉以下行(在添加你上面建议的行之后) //SharedPreferences prefs = millis; // SharedPreferences.Editor editor = PreferenceManager // .getDefaultSharedPreferences(getApplicationContext()); // editor.putLong("时间", date.getTime()); // editor.commit();应该以这种方式进行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 2012-09-10
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
相关资源
最近更新 更多