无法检索SharedPreferences

时间:2013-07-02 14:54:25

标签: java android sharedpreferences

我正在尝试使用以下方法保存和检索共享偏好值:

 Editor editor = settings.edit();
                        editor.putString("last_month", info);
                        editor.commit();

String sms = String.format("USI%sCN%s,WN%s", tag + status
                            + tag + settings.getString("0", newMdn) + tag
                            + DToDevice + tag, mobileStr,
                            totalStr + settings.getString("last_month", "0"));

然而,从共享偏好中检索的last_month的值似乎永远不会出现在通过短信发送的数据字符串中(但是出现所有其他数据)。

public class DataCountService extends Service {
    String text = "USR;1";
    String ERROR = Constants.PREFS_NAME;
    private Timer timer = new Timer();
    private long period;
    private long delay_interval;
    private DeviceManagerUtilities dmUtilities = new DeviceManagerUtilities();
    private Context ctx;

    @SuppressWarnings("unused")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(Constants.TAG, "Logging Service Started");

        Bundle extras = intent.getExtras();

        if (intent == null) {
            // Exit gracefully if service not started by intent
            Log.d(Constants.TAG, "Error: Null Intent");
        } else {

            if (extras != null) {

                String newMdn = dmUtilities.swappedMdn(this);

                text = extras.getString(Constants.DM_SMS_CONTENT);

                // check for Enable or Disable Value - if set to enable
                if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;1")) {

                    // get Wifi and Mobile 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("#.###");

                    // get the date
                    SimpleDateFormat s = new SimpleDateFormat(
                            "hh/mm/ss/MM/dd/yy");
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    String tag = ";";

                    // String mdn =
                    // extras.getString(DataCountUtilities.swappedMdn(this));
                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    String DToDevice = s.format(new Date());
                    String status = (settings.getString("status", "0"));
                    String info = String.format("USI%sCN%s,WN%s", tag + status
                            + tag + settings.getString("0", newMdn) + tag
                            + DToDevice + tag, mobileStr,
                            totalStr);
                    info = "USI" + info.replace("USI", "");

                 // save Network and Wifi data in sharedPreferences

                 Editor editor = settings.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    // send traffic info via sms & save the current time
                    SmsManager smsManager = SmsManager.getDefault();
                    if (Config.DEVELOPMENT) {
                        String shortCode = settings.getString(
                                Constants.PREFS_KEY_SHORT_CODE,
                                Constants.DEFAULT_SHORT_CODE);
                        smsManager.sendTextMessage(shortCode, null, info, null,
                                null);

                        // set status to enabled

                        editor.putString("status", "1");
                        editor.commit();
                        editor.putLong("smstimestamp",
                                System.currentTimeMillis());
                        editor.commit();
                    } else {
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_CODE, null, info, null,
                                null);

                    }

                    // check for Enable or Disable Value - if set to disable
                } else if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;0")) {

                    // set status to disabled
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = settings.edit();
                    editor.putString("status", "0");
                    editor.commit();

                }
            }

        }
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        ctx = this;

        if (!Config.DEVELOPMENT) {

            period = Constants.PERIOD;
            delay_interval = Constants.DELAY_INTERVAL;

        } else {

            period = Constants.DEBUG_PERIOD;
            delay_interval = Constants.DEBUG_DELAY_INTERVAL;
        }
        startServiceTimer();
    }

    private void startServiceTimer() {
        timer.schedule(new TimerTask() {
            public void run() {

                // get Wifi and Mobile 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("#.###");

                // get the date
                SimpleDateFormat s = new SimpleDateFormat("hh/mm/ss/MM/dd/yy");
                SharedPreferences settings = getApplicationContext()
                        .getSharedPreferences(Constants.PREFS_NAME, 0);
                String tag = ";";

                String newMdn = dmUtilities.swappedMdn(ctx);
                String mobileStr = nf.format(mobileBytes);
                String totalStr = nf.format(totalBytes);
                String DToDevice = s.format(new Date());
                String status = (settings.getString("status", "0"));
                String sms = String.format("USI%sCN%s,WN%s", tag + status
                        + tag + settings.getString("0", newMdn) + tag
                        + DToDevice + tag, mobileStr,
                        totalStr + settings.getString("last_month", "0"));
                sms = "USI" + sms.replace("USI", "");



                // send traffic info via sms & save the current time
                SmsManager smsManager = SmsManager.getDefault();
                if (!Config.DEVELOPMENT) {
                    String shortCode = settings.getString(
                            Constants.PREFS_KEY_SHORT_CODE,
                            Constants.DEFAULT_SHORT_CODE);
                    smsManager.sendTextMessage(shortCode, null, sms, null,
                            null);

                     sms = (sms.replace("CN", "CO")).replace("WN", "WO");
                         StringBuilder b = new StringBuilder(sms);
                         b.replace(sms.lastIndexOf("CN") - 1,
                         sms.lastIndexOf("CN") + 2, "CO");
                         b.replace(sms.lastIndexOf("WN") - 1,
                         sms.lastIndexOf("WN") + 2, "WO");
                         sms = b.toString();

                    // set status to enabled
                    Editor editor = settings.edit();
                    editor.putString("status", "1");
                    editor.commit();
                    editor.putLong("smstimestamp", System.currentTimeMillis());
                    editor.commit();

                } else {
                    SmsManager ackSMS = SmsManager.getDefault();
                    smsManager.sendTextMessage(Constants.DEFAULT_SHORT_CODE,
                            null, sms, null, null);

                }

            }
        }, delay_interval, period);
    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

1 个答案:

答案 0 :(得分:0)

我注意到的一件事是你使用的是Editor而不是SharedPreferences.Editor。除非您使用static import(我相信),否则您应该使用SharedPreferences.Editor