2014-03-25 157 views
1

我在我的IntentserviceonHandleIntent裏面有這個while循環,它假設在SharedPreferences中改變一個長整型值。由於某種原因,值保持不變:while循環的問題

@Override 
    protected void onHandleIntent(Intent serviceIntent) { 
     Intent openMain = new Intent(this, Homepage.class); 
      long savedcount = 0; 
     // save row count 

      SharedPreferences sp = PreferenceManager 
        .getDefaultSharedPreferences(NotificationService.this); 
      Editor edit = sp.edit(); 
      edit.putLong("myrowcount", count); 
      edit.commit(); 

     // TODO Auto-generated method stub 
     while(myservice == true){ 
SharedPreferences compareCount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     savedcount = compareCount.getLong("myrowcount", count); 
      JSONParser jsonUpdater = new JSONParser(); 
      JSONObject json = jsonUpdater.getJSONFromUrl(UPDATE_URL); 
      try { 
       count = json.getLong(TAG_ROWCOUNT); 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       if(savedcount>count){ 
        //Store parsed data in sharedpref 
        Log.d(TAG, "savedcount "+savedcount+" > "+count); 
       }else if(savedcount == count){ 
        Log.d(TAG, "savedcount "+savedcount+" = "+count); 
        //Do nothing 
       }else if(savedcount < count){ 
        //send notification and store(override)savedcount with count 
        Log.d(TAG, "savedcount "+savedcount+" < "+count); 
        //notification 
        // ... 
      //override savedcount 
      SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
      Editor newedit = newcount.edit(); 
      newedit.putLong("myrowcount", count); //edited. this solved the problem 
       newedit.commit(); 
       } 
       try { 
        Thread.sleep(300000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }//end of catch 

     }//END OF WHILE LOOP 
     }//END OF onHandleIntent() 

while循環不斷擊打最後一個if語句,其中savedcount < count但第一循環後其假設是等於count。我在這裏做錯了什麼?

////編輯 這是我的代碼如何其餘看起來onhandleintent以上():

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.IntentService; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.preference.PreferenceManager; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 
import android.widget.Toast; 

public class NotificationService extends IntentService { 

    NotificationManager nm; 

    private static final String TAG = NotificationService.class.getSimpleName(); 
    private static final String UPDATE_URL = "http://192.168.1.6/webservice/updatecheck.php"; 
    public static final String TAG_ROWCOUNT = "rowcount"; 
    long count = 0; 
    private boolean myservice = true; 
    static final int uniqueID = 1234; 
    //static final long DELAY = 30000; // 30 seconds 
    public NotificationService() { 
     super("NotificationService"); 
     // TODO Auto-generated constructor stub 

    } 

回答

1

SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
Editor newedit = newcount.edit(); 
edit.putLong("myrowcount", count); 
newedit.commit(); 

在上面的代碼變化edit.putLong( 「myrowcount」,計數); newedit.putLong(「myrowcount」,count);

+0

確實,我想不斷檢查一個值fr om網址並將其存儲在「計數」中。 count標籤不需要改變,因爲json輸出將會改變。我編輯了我的代碼。往上看。 – crushman

+0

您可以在每次通話後發佈該網址的輸出嗎?你是否對每個連續呼叫獲得不同的計數值? – hellboy

+0

不,目前我沒有改變輸出,它總是一個json'{「rowcount」,2}',logcat清楚地顯示了json被收到的記錄, 」。 – crushman

2

的問題似乎在於這裏:while(myservice = true)。該行將始終評估爲真。將其更改爲:while(myservice == true)或簡單地while(myservice)應該可以工作。

+0

謝謝你澄清,但循環仍然每次都碰到同一個地方。也許我應該提到「long count = 0」的初始值我將該值存儲在共享首選項中,然後使用另一個值覆蓋該值,如果該值小於其他值,那麼下一個循環應該在if語句相同的位置進行搜索。 – crushman

+0

@crushman:是否可以調試確保你所得到的值是你期望的值 – npinti

+0

我可以清楚地看到log.d的輸出,它們是正確的,正確的值是進入2的計數,但它的存儲像它沒有被存儲在共享首選項中,該共享首選項的關鍵是「myrowcount」 – crushman

0

我想!!!!爲什麼你正在檢查正面的情況.. myservice會顯示總是正面的真正價值right.so你可以只使用變量的名稱..像while(myservice)或其他while(myservice = true)。 (myservice)或while(myservice = true)或while(1)時更改您的代碼while(myservice == true)

thank you..