I have the same setup in my application using CheckBoxPreference in my activity and getSharedPreference in the onCreate service class. When checked, the service starts. Uncheck the box to stop the service.
Listen and process click preferences in my activity:
getPreferenceManager().findPreference("prefEnable").setOnPreferenceClickListener(new OnPreferenceClickListener() { // not used: Intent intent = new Intent(this, MyService.class); @Override public boolean onPreferenceClick(Preference preference) { CheckBoxPreference cb = (CheckBoxPreference) preference; if (cb.isChecked()) { Log.d(TAG, "User enabled service"); // code to start service } else { Log.d(TAG, "User disabled service"); // code to stop service } return true; } });
Get my prefEnable preference in my onCreate service:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Boolean isEnabled = preferences.getBoolean("prefEnable", false);
Perhaps in your onDestroy you can reuse this and say:
if (isEnabled==false) { *** notification code ***
source share