I can't stop my service through checkboxpreferences.This is the CheckBoxPreference code
CheckBoxPreference checkBoxPref = (CheckBoxPreference) getPreferenceManager().findPreference("firstDependent");
checkBoxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.toString().equals("true")) {
editor.putBoolean("notification_a", true);
editor.commit();
//sendSimpleNotification();
startService(new Intent(settings.this,service.class));
Log.d("Check", "startService from checkbox");
}
else if (newValue.toString().equals("false")){
editor.putBoolean("notification_a", false);
editor.commit();
//mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);
stopService(new Intent(getApplicationContext(),service.class));
Log.d("Check", "stopService from checkbox");
}
return true;
}
});
So if the checkbox is unchecked the service would stops but nothing happen. Why?
public class YOUR_CLASS extends Activity implements View.OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pianostudio);
...CODE...
CheckBox checkBoxPref = (CheckBox)findViewById(R.id.YOUR_CHECKBOX_ID);
checkBoxPref.setOnClickListener(this);
...OTHER CODE...
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.YOUR_ID_CHECKBOX:{ //checkbox per de/selezionare l'idoneita' di un esame
if (checkBoxPref.isChecked()){
editor.putBoolean("notification_a", true);
editor.commit();
//sendSimpleNotification();
startService(new Intent(settings.this,service.class));
Log.d("Check", "startService from checkbox");;
}
else{
editor.putBoolean("notification_a", false);
editor.commit();
//mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);
stopService(new Intent(getApplicationContext(),service.class));
Log.d("Check", "stopService from checkbox");
}
break;
}
}
}
}