Reputation: 1112
i have this while loop inside my onHandleIntent
of my Intentservice
that is suppose to change a long value in SharedPreferences
. for some reason the value stays the same :
@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()
the while
loop keeps hitting the last if statement where savedcount < count
but after the first loop its suppose to be equal to count
. what am i doing wrong here?
////edited this is how the rest of my code looks above the 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
}
Upvotes: 1
Views: 196
Reputation: 2252
SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor newedit = newcount.edit();
edit.putLong("myrowcount", count);
newedit.commit();
In the above code change edit.putLong("myrowcount", count); to newedit.putLong("myrowcount", count);
Upvotes: 1
Reputation: 428
I think!!!! why you are checking positive scenario.. myservice will show in always positive of the true value right.so you could be use only name of the variable.. like while(myservice) or else while(myservice=true). so change your code while(myservice==true) to while(myservice) or while(myservice=true) or while(1);
thank you..
Upvotes: 0
Reputation: 52185
The problem seems to lie here: while(myservice = true)
. That line will always evaluate to true. Changing it to this: while(myservice == true)
or simply while(myservice)
should work.
Upvotes: 2