Question

I'm trying to use user-preferences checkbox to show or not to show the notification on the status bar. So far I have done this :

MainActivity.java

@Override
public void UserPref {

    String notificationTitle = "ASD";
    String notificationMessage = "ASD ASD ASD";

    Intent targetIntent = new Intent(this, MainActivity.class);

    int requestCode = AppSingleton.NOTIFICATION_ID;

    PendingIntent contentIntent = PendingIntent.getActivity(this,
            requestCode, targetIntent, 0);
    String statusBarTickerText = "ASD ASD ASD";
    int icon = R.drawable.ic_launcher;

    Notification notification = new Notification(icon, statusBarTickerText,
            System.currentTimeMillis());
    notification.flags = Notification.FLAG_ONGOING_EVENT
            | Notification.FLAG_NO_CLEAR;
    notification.setLatestEventInfo(this, notificationTitle,
            notificationMessage, contentIntent);

    nm.notify(AppSingleton.NOTIFICATION_ID, notification);
}

I'm able to show the notification all the time. But now I want to add User-Preferences through which the user can disable or enable the notification. Here is my code for PreferenceActivity :

UserPreference.java

public class UserPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.prefs);

    final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
            .findPreference("Checkbox");

    checkboxPref
            .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                     Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());       
                        return true;
                }
            });
   }
}

I'm not able to call the function when the CheckBox is checked from the MainActivity.java but my I'm able to print boolean value in DDMS.

Please see and correct me what I'm doing wrong and help me to overcome this problem.

Was it helpful?

Solution

You shouldn't need to register a listener for the change of preference. You can check for a flag before sending out the notification with the following code by just getting the value of the checkbox as saved in the default shared preferences:

SharedPreferences defaultSettings = PreferenceManager.getDefaultSharedPreferences(this);
boolean notifyEnabled = defaultSettings.getBoolean("Checkbox", true);

if(notifyEnabled) {
    // Perform code to send notification 
}

If you still want to be notified in some way when the checkbox value has changed, you can do so by overriding the onSharedPreference() method:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
    String key)
{
    if(key.compareToIgnoreCase("Checkbox") == 0)
    {
        boolean isChecked = sharedPreferences.getBoolean("Checkbox", false);
        if(isChecked)
        {
            // Checkbox is checked
        }
        else
        {
            // Checkbox has been unchecked
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top