1

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

    }
crushman
  • 1,023
  • 9
  • 21

3 Answers3

2

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thank you for clarifying but the loop still hits the same place everytime. maybe i should mention that the initial value of "long count = 0;' i store that value in a shared preference and then override that value with another value if it is less than that other value. so the next loop should hit the if statement where they are equal. – crushman Mar 25 '14 at 06:08
  • @crushman: Would it be possible to debug to ensure that the values you are getting are the ones you are expecting? – npinti Mar 25 '14 at 06:23
  • i can clearly see the outputs of the log.d, they are correct. the right value is getting into count which is 2. but its like it is not being stored in the shared preference. the key for that shared preference is "myrowcount" – crushman Mar 25 '14 at 06:27
  • @crushman: I've gone through [this](http://stackoverflow.com/questions/18793903/android-sharedpreferences-update-does-not-work) SO post and my understanding is that values of shared preferences do not change. Would it be possible for you to try to first remove the `count` entry and then add it again? – npinti Mar 25 '14 at 06:53
1

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);

hellboy
  • 2,222
  • 1
  • 14
  • 19
  • indeed, i want to constantly check a value from that url and store it in 'count'. the count tag does not need to change because the json output will be changing. i have edited my code. see above. – crushman Mar 25 '14 at 06:16
  • Can you post the output of that url after each call? Are you getting different count value for each successive call? – hellboy Mar 25 '14 at 06:20
  • no, at the moment i am not changing the output it will always be a json '{"rowcount", 2}' , and the logcat clearly shows me the logs that the json is being recieved as a long into count as '2'. – crushman Mar 25 '14 at 06:24
  • OK, another thing I noticed is that the value of savedCount is not changing. The line to assign value in savedCount is sitting outside the while loop. Can you try putting - savedcount = compareCount.getLong("myrowcount", count); as the first line in the while loop – hellboy Mar 25 '14 at 06:30
  • the while loop still hits the last if statement. savedCount is only referencing a stored long in shared preference. would it really matter where it was positioned? i dont see y its not overriding the data at key "myrowcount" – crushman Mar 25 '14 at 06:45
  • savedCount is a variable which is assigned outside the while loop. If the value of that variable is not changed by assigning a new value, the code will hit the same if block. That is the reason, I asked you to put savedcount = compareCount.getLong("myrowcount", count); as the first line in while loop. That would make sure that the value in the savedcount variable is the latest from the preference. By just updating the value in the preference would not automatically reflect in the savedcount variable. Can you update the original post with the change that you made? – hellboy Mar 25 '14 at 07:16
  • Declare savedcount outside while loop. Long savedcount = 0; inside the while loop remove "long" from the line "long savedcount = compareCount.getLong("myrowcount", count);" – hellboy Mar 25 '14 at 14:12
  • post updated. but still same result, the loop hits the last if every time and the log.d gives : 'savedcount 0 < 2' .Is it possible it has to do with the application or service context? just like how Toast normally gives problems when used in a service. – crushman Mar 25 '14 at 15:24
  • 1
    One more change and hope it works. I notice that in the following code - SharedPreferences newcount = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); Editor newedit = newcount.edit(); edit.putLong("myrowcount", count); newedit.commit(); change edit.putLong("myrowcount", count); to newedit.putLong("myrowcount", count);. You are saving the preference in the wrong object – hellboy Mar 25 '14 at 15:55
  • That was it! This typo was the source of the problem all this time! thank alot, i edited the post for future users. wish i could select ur comment as answer. – crushman Mar 25 '14 at 21:29
0

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..
Sethu
  • 430
  • 2
  • 13