Search code examples
androideclipseparse-platformapplication-settings

Disabling parse notifications


I'm using parse.com for push notifications in my app. I added a "disable notifications" option to application settings like this:

<PreferenceCategory android:title="Obvestila" >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="prefNotification"
        android:summary="Za obvestila je potrebna internetna povezava"
        android:title="Prejemal obvestila" />
</PreferenceCategory>

And then added this method to MainActivity:

public void getSettingsPrefs(){
    SharedPreferences settingsPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    notification = settingsPrefs.getBoolean("prefNotification", true);

    if (notification == false)
    {
        PushService.setDefaultPushCallback(this, null);
    }
    else
    {
        PushService.setDefaultPushCallback(this, MainActivity.class);
    }
}

Now the problem is that this only partly works. Once I check or uncheck the box in the settings, I need to completely re-open the MainActivity so that the getSettingsPrefs() is executed in onCreate. How do I make the IF statement execute everytime the checkbox is checked?


Solution

  • Solved this myself by adding OnPreferenceChangeListener.

    notification = (CheckBoxPreference) findPreference("prefNotification");
        notification.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
    
            @Override
            public boolean onPreferenceChange(Preference preference,
                    Object newValue) {
                if (newValue.toString().equals("true"))
                {
                    notificationsOn();
                }
                else
                {
                    notificationsOff();
                }
                return true;
            }
    
        });