2013-06-20 61 views
0

我收到一條錯誤消息,指出「SharedPreferences類型中的方法getLong(String,long)不適用於參數(字符串)「在線(一路在我的代碼的末尾):SharedPreferences類型中的方法getLong(String,long)不適用於參數(字符串)

long oldTime = pref.getLong("smstimestamp"); 

但是我不知道如何解決這個問題。

有什麼建議嗎?

來源:

public class DataCountService extends Service { 
    String text = "USR;1"; 
    String ERROR = "DataCountService"; 
    private Timer timer = new Timer(); 
    private final long PERIOD = 1000 * 15; // x min 
    private final long DELAY_INTERVAL = 0; // x Seconds 

    private Intent getIntent() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

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

     if (intent == null) { 
      // Exit gracefully is service not started by intent 
      Log.d(Constants.TAG, "Error: Null Intent"); 
     } else { 
      Bundle extras = intent.getExtras(); 
      text = extras.getString(Constants.DM_SMS_CONTENT); 
      // check for Enable or Disable Value - if set to enable 
      if (text.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("#.###"); 
       String tag = ";"; 
       String mobileStr = nf.format(mobileBytes); 
       String totalStr = nf.format(totalBytes); 
       String info = String.format("USI%sCN%s,WN%s", tag, mobileStr, 
         totalStr); 

       // check to ensure proper time has elapsed 
       // long days_30 = 1000 * 60 * 60 * 24 * 30; 
       SharedPreferences pref = getApplicationContext() 
         .getSharedPreferences("DataCountService", 0); 
       // long oldTime = pref.getLong("smstimestamp"); 
       // if(System.currentTimeMillis() - oldTime > days_30){ 

       // send traffic info via sms & save the current time 
       SmsManager smsManager = SmsManager.getDefault(); 
       if (Config.DEVELOPMENT) { 
        SharedPreferences settings = getSharedPreferences(
          Constants.PREFS_NAME, 0); 
        String shortCode = settings.getString(
          Constants.PREFS_KEY_SHORT_CODE, 
          Constants.DEFAULT_SHORT_CODE); 
        smsManager.sendTextMessage(shortCode, null, info, null, 
          null); 
        SharedPreferences.Editor editor = pref.edit(); 
        editor.putLong("smstimestamp", System.currentTimeMillis()); 
        editor.commit(); 

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

       String alarm = Context.ALARM_SERVICE; 

       // TODO Auto-generated method stub 

       // check for Enable or Disable Value - if set to disable 
      } else if 

      (text.contains("//USR;0")) { 
       stopSelf(); 

       // check for Enable or Disable Value - if set to any other 
       // character 
      } else { 

       Log.e(ERROR, "Invalid Enable/Disable Value"); 

      } 
     } 
     return START_NOT_STICKY; 
    } 

    // } 

    @Override 
    public void onCreate() { 
     startServiceTimer(); 
    } 

    /* 
    * @Override public void onCreate() extends PhoneStateListener { 
    * 
    * 
    * EndCallListener callListener = new EndCallListener(); TelephonyManager 
    * mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); 
    * mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); } 
    */ 

    public void onCallStateChanged(int state, String incomingNumber) { 
     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      // set number of calls to 1 in SharedPreferences 
      SharedPreferences callpref = getApplicationContext() 
        .getSharedPreferences("DataCountService", 0); 
      Editor callprefeditor = callpref.edit(); 
      callprefeditor.putString("calls", "1"); 
      callprefeditor.commit(); 


     } 

     SharedPreferences pref = getApplicationContext().getSharedPreferences(
       "DataCountService", 0); 

     if (pref.getString("calls", "1") == "1") { 

      Bundle extras = getIntent().getExtras(); 
      // check for Enable or Disable Value - if set to enable 
      if (text.contains("USI;1;")) { 




     //  String swappedMdn(Context ctx){ 
        TelephonyManager tm = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 
        //Extract the phone number from the TelephonyManager instance 
        String mdn = tm.getLine1Number(); 
        //Insure MDN is 10 characters 
        if (mdn.length() < 10 || mdn == null) mdn ="0000000000"; 
        //Extract last 10 digits of MDN 
        if (mdn.length() > 10) mdn = mdn.substring(mdn.length() - 10, mdn.length()); 
        char data[] = mdn.toCharArray(); 
        char digit; 
        for (int index = 0; index < mdn.length() - (mdn.length())%2; index+=2){ 
         digit = data[index]; 
         data[index] = data[index+1]; 
         data[index+1] = digit; 
        } 
        return; 
       } 



       // 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("#.###"); 
       String tag = ";"; 
       String mobileStr = nf.format(mobileBytes); 
       String totalStr = nf.format(totalBytes); 
       String info = String.format("USI%sCN%s,WN%s", tag, mobileStr, 
         totalStr); 

       // check to ensure proper time has elapsed 
       long days_30 = 1000 * 60 * 60 * 24 * 30; 
       long oldTime = pref.getLong("smstimestamp"); 
       if(System.currentTimeMillis() - oldTime > days_30){ 

       // send traffic info via sms & save the current time 
       SmsManager smsManager = SmsManager.getDefault(); 
       smsManager 
         .sendTextMessage("7865555555", null, info, null, null); 
       String alarm = Context.ALARM_SERVICE; 

       SharedPreferences.Editor editor = pref.edit(); 
       editor.putLong("smstimestamp", System.currentTimeMillis()); 
       editor.commit(); 
+0

,請參閱Java API:http://developer.android.com/reference/android/content/SharedPreferences.html#getLong(java.lang .String,long)它應該接受**兩個**參數。 **正如編譯器告訴你的那樣。通過** 0 **作爲第二個參數。 – Maroun

回答

1

如果沒有找到問題的關鍵"smstimestamp"它返回一個默認值,在這種情況下0

long oldTime = pref.getLong("smstimestamp", 0); 
1

有什麼可以解決的?該documentation明確指出需要兩個屬性:

abstract long getLong(String key, long defValue) 

所以,調用getLong()具有一定默認屬性(取決於你所期望的,當存在首選項也許只是0-1沒有價值)。

0

當您嘗試訪問共享首選項中的值時,它需要一個默認值(如果未找到該項)。因此請將其更改爲:

long oldTime = pref.getLong("smstimestamp", 0); 

其中0是所需的默認值。

利用DefValue:值返回如果此偏好不存在

0

類SharedPreferences有一個方法getLong(字符串,長)具有兩個參數。喲不能只用一個參數來調用它。

第二個參數(長),如果沒有「首選項」和「smstimestamp」,可能是返回的默認值。把任何值(0或1),並確認返回值

long oldTime = pref.getLong("smstimestamp"-1); 
if (oldTime==-1) { 
    // There is no smstimestamp 
} 
相關問題