whiteLT
whiteLT

Reputation: 338

Getting Static integer from Service

I have activity and service, I would like to get a reference to Service integer, that is being updated from time to time in Service. My problem is that in my Activity I only get that integer first declared Value (for instance 0).

My main goal is to know Services' updated value every time I start my program.

Main activity:

if(Service.doesCounter>0){
                        //do something
                //in this state Service.doesCounter always is 0(checked by log)
        }

Service:

public static int doesCounter=0; // declared after class as class memeber
//code where I start my method does();

.....
public void does(){
        doesCounter++;
        Log.e("cccccc","Service Counter "+doesCounter); // everything ok, value is changing as suppose to.
}

Edit

my Shared Preferences class:

public class AppPreferences extends PreferenceActivity {
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; //  Name of the file -.xml
         private SharedPreferences appSharedPrefs;
         private Editor prefsEditor;

         public AppPreferences(Context context)
         {
             this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
             this.prefsEditor = appSharedPrefs.edit();
         }

         public boolean getAnything() {
             return appSharedPrefs.getBoolean("Anything", false);

         }

         public void setAnything(Boolean text) {
             prefsEditor.putBoolean("Anything", text);
             prefsEditor.commit();
         }

Then from Main Activity:

public class MainActivity extends Activity {
    protected AppPreferences appPrefs;
appPrefs = new AppPreferences(getApplicationContext());
appPrefs.setAnything(fasle);

Then from Service:

appPrefs = new AppPreferences(getApplicationContext());

And when this happens all earlier made changes are reseted, how to make service and MainActivity use same prefs? Maybe I can somehow make AppPrefs class static?

Upvotes: 0

Views: 61

Answers (1)

fedepaol
fedepaol

Reputation: 6862

Using static class fields is considered a bad practice in android. Your app's resources may be revoked by the os and another process of your app may be re-initialized whenever the user gets back to it. In this case you will loose doesCounter updates. I don't know if this is the case (it should work in a common scenario where your app is foregrounded, unless you are running your service in another process (using the flag isolatedProcess) .

The easiest way to achieve what you are trying to do "the android way" is to store the doesCounter in SharedPreferences.

One way to achieve that is having a static class like this:

public class PrefUtils {
private final static String NUM_DOES = "NumDoes";

public static int getNumDoes(Context c)
{
    int mode = Activity.MODE_PRIVATE;
    SharedPreferences mySharedPreferences = c.getSharedPreferences(PREF_NAME, mode);        
    return mySharedPreferences.getInt(NUM_DOES, 0);
}


public static void setNumDoes(int numDoes , Context c)
{
    int mode = Activity.MODE_PRIVATE;
    SharedPreferences mySharedPreferences = c.getSharedPreferences(PREF_NAME, mode);        
    SharedPreferences.Editor editor = mySharedPreferences.edit();   
    editor.putInt(NUM_DOES, numDoes);
    editor.commit();

}

And you are done. Just call PrefUtils.getNumDoes / setNumDoes

Upvotes: 1

Related Questions