Android - CheckBoxPreference dependent on SwitchPreference doesn't get set to FALSE after Switch is turned OFF -
i have settingsactivity
implements sharedpreferences.onsharedpreferencechangelistener
. loads preferences fragments (as suggested latest android developer's guide).
of course override onsharedpreferencechanged(...)
, switch(key)
take care of .
since in preferences.xml
declared checkboxpreference
dependent on switchpreference
's state, whenever user disables switch following happens:
- the checkbox gets disabled (which happens already)
- the corresponding sharedpreference set false
- the corresponding case in
onsharedpreferencechanged(...)
gets triggered (in order take care of killing things related it)
what's sense of disabling checkbox parent, if it's related preference doesn't updated well?
i tried
boolean clipboardservicebootstatus = sharedpreferences.getboolean(key_pref_clipboard_service_boot, false); if (clipboardservicebootstatus == true){ log.d("settings activity", "boot enabled too. disabling now"); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putboolean(settingsactivity.key_pref_clipboard_service_boot, false); editor.commit();
but checkbox remains checked (while grayed ) , strange things happens subsequent checkbox's clicks being detected twice or trice after being re-enabled
settingsactivity
public class settingsactivity extends preferenceactivity implements sharedpreferences.onsharedpreferencechangelistener { public static final string key_pref_clipboard_service = "clipboard_listener"; public static final string key_pref_clipboard_service_boot = "clipboard_listener_boot"; private string user_id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); preferencemanager.getdefaultsharedpreferences(this).registeronsharedpreferencechangelistener(this); // display fragment main content. getfragmentmanager().begintransaction() .replace(android.r.id.content, new settingsfragment()) .commit(); } @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { switch (key){ case key_pref_clipboard_service: boolean clipboardservicestatus = sharedpreferences.getboolean(key_pref_clipboard_service, false); log.d("settings activity", "clipboard service status turned to: "+clipboardservicestatus); if (clipboardservicestatus == true){ //toggled on if (!ismyservicerunning(clipboardservice.class)){ //service not running. start service intent serviceintent = new intent(this, clipboardservice.class); startservice(serviceintent); } else { log.d("settings activity", "strange, user started service, service running"); } } else { //toggled off if (ismyservicerunning(clipboardservice.class)) { //service running. shutting down intent stopintent = new intent(this, clipboardservice.class); stopservice(stopintent); } else { log.d("settings activity", "strange, user stopped service, service stopped"); } // take care of disabling checkbox // ... } break; case key_pref_clipboard_service_boot: //do smth boolean clipboardservicebootstatus = sharedpreferences.getboolean(key_pref_clipboard_service_boot, false); log.d("settings activity", "clipboard service boot status turned to: "+clipboardservicebootstatus); packagemanager pm = getapplicationcontext().getpackagemanager(); componentname componentname = new componentname(settingsactivity.this, bootcompletedreceiver.class); if (clipboardservicebootstatus == true){ //service @ boot turned on //enabling broadcast receiver pm.setcomponentenabledsetting(componentname, packagemanager.component_enabled_state_enabled, packagemanager.dont_kill_app); } else { //service @ boot turned off //disabling broadcast receiver pm.setcomponentenabledsetting(componentname, packagemanager.component_enabled_state_disabled, packagemanager.dont_kill_app); } break; default: throw new illegalargumentexception("invalid setting: " + key); } } public boolean ismyservicerunning(class<?> serviceclass) { activitymanager manager = (activitymanager) getsystemservice(context.activity_service); (runningserviceinfo service : manager.getrunningservices(integer.max_value)) { if (serviceclass.getname().equals(service.service.getclassname())) { return true; } } return false; } }
settingsfragment
public class settingsfragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); } }
preferences.xml
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <switchpreference android:key="clipboard_listener" android:title="clipboard listener" android:summaryoff="turn on start background service" android:summaryon="turn off stop background service" android:defaultvalue="false" /> <checkboxpreference android:key="clipboard_listener_boot" android:dependency="clipboard_listener" android:title="clipboard listener @ startup" android:summaryoff="check start background service @ every boot" android:summaryon="uncheck disable background service @ boot " android:defaultvalue="false" /> </preferencescreen>
Comments
Post a Comment