2013-07-02 140 views
0

我試圖保存和使用檢索sharedpreferences值:無法檢索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的價值正在從sharedpreferences檢索似乎永遠不會出現在通過短信發送的數據字符串(但是會顯示所有其他數據)。

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

    } 

} 

回答

0

只有一兩件事我注意到的是,您使用的是Editor而非SharedPreferences.Editor。除非你使用的是static import(我相信),你應該使用一個SharedPreferences.Editor