0

我有下面這個方法從用戶的設備將消息發送到服務器。我要救我發送的消息在SharedPreferences服務器的日期,並檢查是否有新郵件進來,他們比SharedPreference日期目前然後我發的消息。我如何去做。的Android檢查當前日期以前的日期保存在SharedPreference

我更新了我的問題與我如何檢查日期,但我知道檢查日期的方法不工作,因爲任何時候我打電話給這種方法消息已經上傳到服務器繼續上傳,但我只希望他們上傳一次,除非新郵件進入

private void startSync() { 
    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox"); 
    Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null); 
    startManagingCursor(cursor1); 
    String transaction = ""; 
    String[] columns = new String[] { "address", "person", "date", "body","type" }; 
    if (cursor1.getCount() > 0) { 
     String count = Integer.toString(cursor1.getCount()); 
     while (cursor1.moveToNext()){ 
      String address = cursor1.getString(cursor1.getColumnIndex(columns[0])); 
      String name = cursor1.getString(cursor1.getColumnIndex(columns[1])); 
      Date date = new Date(cursor1.getLong(0)); 
      formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date); 
      //date = cursor1.getString(cursor1.getColumnIndex(columns[2])); 
      String msg = cursor1.getString(cursor1.getColumnIndex(columns[3])); 
      type = cursor1.getInt(cursor1.getColumnIndex(columns[4])); 
      status = 1; 
      CreatedBy = userName; 
      ModifiedBy = userName; 

      if (address.equals("MobileMoney")){ 

       transaction += msg; 

       long MILLIS_PER_DAY = 1000 * 60 * 60 * 24; 
       long updDate = prefs.getLong("lastupdate", 0)/MILLIS_PER_DAY; 
       long currdate= System.currentTimeMillis()/MILLIS_PER_DAY; 
       if (!(updDate == currdate)) { 
        saveExternalTransactions(userId, type, status, transaction, formattedDate, CreatedBy, ModifiedBy); 
        SharedPreferences.Editor editor = prefs.edit(); 
        editor.putLong("lastupdate", currdate); 
        editor.commit(); 
       } 

      } 
     } 
    } 


} 
+0

我無法理解有什麼問題。你不知道如何將日期保存到共享前綴中?或者如何比較日期?或者是什麼? –

+0

@VladMatvienko所以你明白我的意思 –

+0

你是在比較錯誤的日期,我更新了我的問題。 !(updDate == currdate)總是如此。 –

回答

0

您需要更換此行

long updDate = prefs.getLong("lastupdate", 0)/MILLIS_PER_DAY; 

此:

long updDate = prefs.getLong("lastupdate", 0); 

它可以解決你的問題

相關問題