为什么这个值为null?

时间:2016-02-11 00:54:12

标签: java android arrays database string

我已成功制作,保存并从mainActivity检索我的共享偏好设置,但我无法从我的服务中获取...

出于某种原因,当我尝试从后台服务中检索时,我的共享首选项为空...

我在onCreate中初始化我的偏好设置:

contactsPrefs = getSharedPreferences("contactsPrefs", MODE_PRIVATE); //Making a shared preferences

在onCreate中保存值:

    myEditor = contactsPrefs.edit();

    Set<String> set = new HashSet<String>();
    set.addAll(contactArrayList);

    myEditor.clear(); //Clearing current values in shared pref
    myEditor.putStringSet("contactSetKey", set); //Adding contacts
    myEditor.commit();

所有这一切都很顺利,但当我尝试从我的服务中访问我的偏好时,我得到null:

    preferences = PreferenceManager.getDefaultSharedPreferences(c); //See edit at bottom for more info
if(preferences.getStringSet("contactSetKey", null) != null) {
        contactArrayList.addAll(preferences.getStringSet("contactSetKey", null));

        for (String number : contactArrayList) {

            number.substring(number.indexOf("-") + 1); //Remove all characters before the hyphen from my string

            Log.v(TAG, number);

        }
    }else{
        Log.v(TAG, "Dagnabit it its null");
    }

令我失望的是,我得到了日志Dagnabit it its null。为什么它是空的?我可以向您保证,它可以在我的主要活动中运行,因为当我从共享首选项访问它时,我可以显示我的共享首选项中的所有数据。所以我知道它不应该是空的......但是出于某种原因

谢谢,

Ruchir

编辑:

我实际上是使用内容观察者注册一个卷监听器,我从那里访问首选项。这是在服务中:

 mSettingsContentObserver = new volumeCheck(this, new Handler());
        getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);

以下是内容观察员:

   public class volumeCheck extends ContentObserver {

 public volumeCheck(Context c, Handler handler) {
        super(handler); //Creates a new handler
        context = c; //variable context, defined earlier, is set equal to c, context of service.

        preferences = PreferenceManager.getDefaultSharedPreferences(c);

}

1 个答案:

答案 0 :(得分:0)

尝试使用应用程序上下文获取共享的pref引用,如下所示:

contactsPrefs = 
          getApplicationContext().getSharedPreferences("contactsPrefs", 
          MODE_PRIVATE); //Making a shared preferences

注意: 如果仍然为null,则从服务的onStart()方法获取共享首选项,而不是在onCreate()中执行。

编辑: 我测试了它的工作

public class MainActivity extends AppCompatActivity {

private SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("Status","Success");
    editor.apply();

    getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,
            new VolumeCheck(this, new Handler()));
}
}



import android.content.Context;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;


public class VolumeCheck extends ContentObserver {
private final SharedPreferences preferences;

private Context context;

public VolumeCheck(Context c, Handler handler) {
    super(handler); //Creates a new handler
    context = c; //variable context, defined earlier, is set equal to c, context of service.

    preferences = PreferenceManager.getDefaultSharedPreferences(c.getApplicationContext());
    String status = preferences.getString("Status", "");
    if(!status.equalsIgnoreCase(""))
    {
        // got the value read from shared preference
        Log.i("Reading value", status);
    }
}
}