PreferenceManager不会更改“偏好设置”选择的值

时间:2019-07-19 23:47:43

标签: java android sharedpreferences

我正在制作一个使用首选项并允许用户选择是否仅希望仅使用Wifi进行数据传输的应用。我遵循了文档 Manage Network Usage 但是不建议使用某些说明,因此我不得不即兴创作。仍然是示例一部分的一件事是 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");

它应该默认为手机的默认网络设置,直到用户对其进行更改为止,或者在我的情况下,我选择将“ any”选项设置为默认,但不会保存更改。

我听不懂什么,也找不到网上直接的人。我在代码中错过了什么?

公共类SettingsActivity扩展了AppCompatActivity {

static final String TAG = "SettingsActivity";
public static final String WIFI = "WiFi";
public static final String FIRST = "First";

public static final String SHARED_PREFS = "sharedPrefs";
public static final String WIFI_CONNECTION = "WiFi";
public static final String CELLULAR_CONNECTION = "Cellular";
public static final String WIFI_PREFERENCE = "WiFi Enabled";
public static final String NO_PREFERENCE = "First Enabled";

public static final String NO_CONNECTION = "No Connection";

public static final String PREF_NETWORK_TYPE = "network_choice";

public static String sPref = null;

private NetworkCheck networkCheck = new NetworkCheck();
private static boolean prefWifi = false;
private static boolean noPreferredNetwork = true;
private static boolean wifiConnected = false;
private static boolean cellularConnected = false;
public static boolean networkConnected = true;

//TODO: Figure out what this needs and implement it. How do I fix the settings menu?

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SettingsFragment())
            .commit();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    PreferenceManager.setDefaultValues(this, R.xml.root_preferences, false);

    IntentFilter filter = new IntentFilter(CONNECTIVITY_SERVICE);
    networkCheck = new NetworkCheck();
    this.registerReceiver(networkCheck, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (networkCheck != null) {
        this.unregisterReceiver(networkCheck);
    }
}

@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this, prefWifi + " " + noPreferredNetwork, Toast.LENGTH_LONG).show();
}

@Override
public void onStart() {
    super.onStart();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");
    Toast.makeText(this, prefWifi + " " + noPreferredNetwork, Toast.LENGTH_LONG).show();

    //checkNetworkFlags(this);
    networkType();
}

public void networkType() {
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(WIFI_PREFERENCE, prefWifi);
    editor.putBoolean(NO_PREFERENCE, noPreferredNetwork);
    editor.putBoolean(NO_CONNECTION, networkConnected);
    editor.apply();
}
public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }
    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.equals(PREF_NETWORK_TYPE)) {
            prefWifi = WIFI.equals(sPref);
            noPreferredNetwork = FIRST.equals(sPref);
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

在“共享”首选项中完成数据编辑后,您需要在完成后调用属性.commit,否则它将不保存日期,希望这对您有帮助:D

答案 1 :(得分:1)

您保存或将数据保存到与您读取或获取的数据不同的sharedPreference。

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);

与 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

选择在整个活动或应用程序中使用