我在我的Intentservice
的onHandleIntent
裏面有這個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
}
確實,我想不斷檢查一個值fr om網址並將其存儲在「計數」中。 count標籤不需要改變,因爲json輸出將會改變。我編輯了我的代碼。往上看。 – crushman
您可以在每次通話後發佈該網址的輸出嗎?你是否對每個連續呼叫獲得不同的計數值? – hellboy
不,目前我沒有改變輸出,它總是一個json'{「rowcount」,2}',logcat清楚地顯示了json被收到的記錄, 」。 – crushman