Question

Where have i to "call" my notification? I want that appears when click the checkboxpreferenced . edit MainActivity.java

import...//here all imports i need

public class MainActivity extends Activity {
CheckBoxPreference firtsDependent;
...
public void onCreate(Bundle savedInstanceState){
  super onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //and your code
}
}
    private void sendSimpleNotification(){ 

        boolean pref_opt1= PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getBoolean("firstDependent", false);

        if(pref_opt1) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(service.this);
            notificationBuilder.setContentTitle("Title");
            notificationBuilder.setContentText("Context");
            notificationBuilder.setTicker("TickerText");
            notificationBuilder.setWhen(System.currentTimeMillis());
            notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);

            Intent notificationIntent = new Intent(this, service.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            notificationBuilder.setContentIntent(contentIntent);
            notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
            mNotificationManager.notify(1, notificationBuilder.build());
        } 
        else{
            mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);      
        }

    }

the settings.java

import..//all imports i need
public class settings extends PreferenceActivity{
public void onCreate(Bundle savedInstanceState){
  super onCreate(savedInstanceState);
  setContentView(R.xml.settings);
  }
}

this is the structure of my code..and of course i have a xml where inside there is the checkboxpreferences with id and key firstDependent.

end edit. I tried in onResume and works only if i go out from preferences screen. How can i do what i want?

Was it helpful?

Solution

You'll need a checkbox listener like this one:

cb = (CheckBox)findViewById(R.id.checkBox);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
      sendSimpleNotification();
   }
}

For checkbox preferences use:

final CheckBoxPreference cbPR= (CheckBoxPreference) getPreferenceManager().findPreference("cbPref");

cbPR.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        sendSimpleNotification();
        return true;
    }
});

OTHER TIPS

in your PreferenceActivity onCreate, register a preference change listener:

getSharedPreferences("YourPrefereceFile", 0).registerOnSharedPreferenceChangeListener(this);

then, make your activity implements the OnSharedPreferenceChangeListener.

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    //Test on the key to know what preference has changed and do what ever you want

}

you code modified:

import..//all imports i need
public class settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{
    public void onCreate(Bundle savedInstanceState){
      super onCreate(savedInstanceState);
      setContentView(R.xml.settings);
         PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
      }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
        // test on your wanted key preference
        // Call your notification methode

    }    
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top