我在我的onHandleIntent
的{{1}}内有这个while循环,假设要更改Intentservice
中的长值。由于某种原因,价值保持不变:
SharedPreferences
@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
。我在这做错了什么?
////编辑 这是我的其余代码在onhandleintent()上面的方式:
count
答案 0 :(得分:2)
问题似乎在于:while(myservice = true)
。该行将始终评估为true。将其更改为:while(myservice == true)
或简称while(myservice)
应该有效。
答案 1 :(得分:1)
SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor newedit = newcount.edit();
edit.putLong("myrowcount", count);
newedit.commit();
SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor newedit = newcount.edit();
edit.putLong("myrowcount", count);
newedit.commit();
在上面的代码中更改edit.putLong(“myrowcount”,count); to newedit.putLong(“myrowcount”,count);
答案 2 :(得分:0)
我想!!!!为什么你正在检查积极的情况..我的服务将始终显示正确的真值。所以你可以只使用变量的名称..像while(myservice)或者while(myservice = true)。 所以将(myservice == true)的代码更改为while(myservice)或while(myservice = true)或while(1);
thank you..