Can't access SharedPreferences from my Service

心已入冬 提交于 2019-12-12 23:40:07

问题


What I'm trying to do


Hello Guys.

I got a Service which, set's a boolean to true or to false whenever the service is started or stopped (started = true / stopped = false) in the SharedPreference. Now when I try to get the Boolean out there in my Activity, it allways dosn't find it. How can I solve this... Here's the Code for you Guys.

Code


Methode in my Service:

private void setStarted(boolean started) {
    // SharedPreferences casten

    mPrefs = this.getSharedPreferences(LOG_TAG, MODE_PRIVATE);
    // Boolean in SharedPreferences hinzufügen
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.clear().apply();
    editor.putBoolean(PREF_STARTED, started).commit();
    editor.commit();

    //mPrefs.edit().putBoolean(PREF_STARTED, started).commit();

    Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}

In my Activity

// mPrefs caten
        mPrefs = this.getSharedPreferences(GPSService.LOG_TAG, MODE_PRIVATE);

        // boolean holen ob service gestartet oder nicht
        run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);

How do I get the boolean out of there? It allways returns me the default value I had to give in the getBoolean Methode.

Thanks for your help in advance

safari


回答1:


Here is some code that I'm successfully using in one of my apps. It is used in various parts of the app, e.g. both from activities and services:

void putValue(Context context, String pref, boolean value) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean(pref, value);
    editor.commit();                
}

boolean getValue(Context context, String value, boolean defaultValue) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    return settings.getBoolean(value, defaultValue);
}



回答2:


Try using getDefaultSharedPreferences(Context context) method of PreferenceManager in both your services and your activities.

private void setStarted(boolean started) {

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    mPrefs.edit().putBoolean(PREF_STARTED, started).commit();

    Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}

In your Activity

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);    
run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);

Also make sure you :

  • Never call .clear() on editor.
  • You use PreferenceManager.getDefaultSharedPreferences(this) everywhere.



回答3:


Just build your prefs object the same way you would as part of your activity. The only difference is pulling your application's context in.

SharedPreferences prefs = _ getApplicationContext().getSharedPreferences("com.example.appname", Activity.MODE_PRIVATE);

Boolean tmpBool = prefs.getBoolean("PREF_NAME", null);


来源:https://stackoverflow.com/questions/10755636/cant-access-sharedpreferences-from-my-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!