Reputation: 342
I am making an app that uses a Preferences and allows the user to pick if they want to specifically only use Wifi Only for data transfers. I followed the documentation
Manage Network Usage
but some of the instructions were deprecated so I had to improvise. One thing that was still part of the example was
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");
And it's supposed to default to the phone's default network settings until the user changes them or in my case, I chose for the "any" option to be default but it doesn't save the change.
I don't understand something and I can't find a straight online. What did I miss in the code?
public class SettingsActivity extends 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);
}
}
}
}
Upvotes: 1
Views: 924
Reputation: 21
Your saving or putting your data to a different sharedPreference from the one your reading or getting from.
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
is different from SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Choose on to use throughout your activity or application
Upvotes: 1
Reputation: 216
When you finish editing the data inside a Shared preference you need to call the property .commit after finish or it'll not save the date, I hope this can help you :D
Upvotes: 1