0

我无法通过 checkboxpreferences 停止我的服务。这是 CheckBoxPreference 代码

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;
              }
        });

因此,如果未选中该复选框,服务将停止但没有任何反应。为什么?

4

1 回答 1

1
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;
         }
     }
}
}
于 2013-08-12T01:18:54.863 回答