简单的问题。我做了一个settingsActivity
并且做了一个sharedPrefernecesLIstener
。它运行良好,所以我独自留下来制作其余的应用程序。我回到它来完成一些事情,发现它停止工作了。我浏览了Stack Overflow
上的一些问题并阅读了一些内容,但没有答案。日志说:
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
搜索此内容不会给我答案,因为答案通常是针对我无法解决问题的特定实例。
例如:An Input is Disposed。我不在我的深度之内,所以我转向询问这个问题。
public class SettingsActivity extends AppCompatActivity {
static final String TAG = "SettingsActivity";
public static final String SHARED_PREFS = "sharedPrefs";
public static final String PREF_NETWORK_TYPE = "network_choice";
public static final String WIFI = "WiFi";
public static final String FIRST = "First";
public static final String WIFI_PREFERENCE = "WiFi Enabled";
public static final String NO_PREFERENCE = "First Enabled";
public String sPref = null;
private NetworkCheck networkCheck;
SharedPreferences sharedPrefs;
SharedPreferences.OnSharedPreferenceChangeListener listener;
private boolean prefWifi = false;
private boolean noPreferredNetwork = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
networkCheck = new NetworkCheck();
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);
this.registerReceiver(networkCheck, filter);
if (savedInstanceState == null) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");
if (sPref == null) {
sPref = FIRST;
}
switch (sPref) {
case WIFI:
prefWifi = true;
noPreferredNetwork = false;
break;
case FIRST:
prefWifi = false;
noPreferredNetwork = true;
break;
default:
prefWifi = false;
noPreferredNetwork = true;
}
}
Log.i(TAG, "onSharedPreferenceChanged: onCreate - " + sPref + " " + prefWifi + " " + noPreferredNetwork);
listener = (sharedPreferences, key) -> {
if (key.equals(PREF_NETWORK_TYPE)) {
Toast.makeText(SettingsActivity.this, "Tell me this works", Toast.LENGTH_LONG).show();
}
};
listener = (sharedPreferences, key) -> {
if (key.equals(PREF_NETWORK_TYPE)) {
sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");
if (sPref == null) {
sPref = FIRST;
}
prefWifi = sPref.equals(WIFI);
noPreferredNetwork = sPref.equals(FIRST);
if (prefWifi) {
sPref = WIFI;
} else if (noPreferredNetwork) {
sPref = FIRST;
}
networkType();
Log.i(TAG, "onSharedPreferenceChanged: in listener- " + sPref + " " + prefWifi + " " + noPreferredNetwork);
}
};
networkType();
}
@Override
public void onDestroy() {
super.onDestroy();
if (networkCheck != null) {
this.unregisterReceiver(networkCheck);
}
}
@Override
public void onResume() {
super.onResume();
sharedPrefs.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void onPause() {
super.onPause();
networkType();
Log.i(TAG, "onSharedPreferenceChanged: onPause- " + sPref + " " + prefWifi + " " + noPreferredNetwork);
sharedPrefs.unregisterOnSharedPreferenceChangeListener(listener);
}
public void networkType() {
sharedPrefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean(WIFI_PREFERENCE, prefWifi);
editor.putBoolean(NO_PREFERENCE, noPreferredNetwork);
editor.apply();
Log.i(TAG, "onSharedPreferenceChanged: networkType- " + " " + "prefWifi: " + prefWifi + " " + "noPreferredNetwork: " + noPreferredNetwork);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
有趣的是,List Preference
在我下次打开应用程序时有效,但是Listener
不起作用。
更新:
下面的root_preferences
是调用PREF_NETWORK_TYPE
变量的地方。或者应该被调用。
<PreferenceCategory
android:key="network"
android:title="Network">
<ListPreference
android:key="network_choice"
android:title="Network Connection Type"
android:summary="Choose the Network connection you to use to access the Internet"
android:entries="@array/pref_connectionType"
android:entryValues="@array/pref_connectionType_values"
android:defaultValue="First"/>
</PreferenceCategory>