1

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?

David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

0

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;
    }
});
g00dy
  • 6,752
  • 2
  • 30
  • 43
  • so in my case, how can i do it? it's almost 3 mounth that i'm looking for a answere for this problem. Please can you help me? have i to insert all `sendSimpleNotification` code into `onCheckedChanged`? – David_D Aug 01 '13 at 12:09
  • Well, put the code I pasted you in the `onCreate()` method (this will set the listener at the beginning. When the checkbox is clicked, then the code inside `onCheckedChanged` will be executed. You can elaborate it with some if statements if you like, to suit you best. – g00dy Aug 01 '13 at 12:14
  • you mean something like: `cb = (CheckBox)findViewById(R.id.checkBox); cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { sendSimpleNotification(); } }` In the onCreate? I don't know if i have understand right. – David_D Aug 01 '13 at 12:19
  • is the same thing if is a checkboxpreferences instead checkbox? – David_D Aug 01 '13 at 12:26
  • Thank, now i try..only one problem with `getPreferenceManager`.. it says unknow method – David_D Aug 01 '13 at 12:40
  • Maybe i've wrong something..your checkboxpreference code have i to put it into preference screen Activity or in my MainActivity? – David_D Aug 01 '13 at 13:03
  • Paste this code wherever the `sendSimpleNotification()` function is defined- e.g. where you want it to appear. If the preference screen Activity is a separate activity and this function is called there, then include it there. – g00dy Aug 01 '13 at 13:15
  • I mean, the notification code i pasted in the first post is in my MainActivity..if i paste your code in the onCreate in my MainActivity i get an error `unknown method getPreferenceManager`..i don't know how resolve it – David_D Aug 01 '13 at 13:38
  • and of course the checkboxpreferences is inside a settings.xml..if i write the notification code and your in the settings.java i don't get any error but when i try to check the checkbox the application crash – David_D Aug 01 '13 at 13:42
  • `import android.preference.*` maybe? – g00dy Aug 01 '13 at 13:43
  • Is your settings.java extending Activity? – g00dy Aug 01 '13 at 13:44
  • `public class settings extends PreferenceActivity` this is the beginning of settings.java – David_D Aug 01 '13 at 13:52
  • How do you go to that class, are you using Intents or not, paste the code for it? Thanks – g00dy Aug 01 '13 at 14:16
  • I have update the question. Where i wrote "//your code" of course i have your `checkbox preferences` code and i get the error in `getPreferenceManager` – David_D Aug 01 '13 at 14:40
  • What about the import i mentioned above? Do you also have it? – g00dy Aug 01 '13 at 15:01
  • Yes! It's so strange.. if i call the `sendSimpleNotification()` in the `onResume` it goes.. The notification appears in the right moment i exit from preferences..But of course i need that the notification appears at the moment i check the checkbox. i think you understand. It's really about 3 mounths i worked with this problem!! – David_D Aug 01 '13 at 15:27
  • You add it in the `onResume()` of which class exactly? You should be able to put it in the `onCreate()` of the `settings class`. If you're still getting the problem with the `getPreferenceManager`, then you should tell me how you are starting the settings class. Where do you enter, how do you create the Intent etc. – g00dy Aug 01 '13 at 15:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34646/discussion-between-g00dy-and-david-d) – g00dy Aug 01 '13 at 18:23
0

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

    }    
    }
  • is it for checkboxpreference? – David_D Aug 01 '13 at 13:38
  • this is valid for all your preferences, and it should work for your checkboxpreference since it stores a boolean in the SharedPreferences – user1897934 Aug 01 '13 at 13:56
  • can you show me with my code please?sorry but i can't understand anything right now. Hope you help me – David_D Aug 01 '13 at 13:57
  • are you using a prefreceActivity? if yes just put the fowllowin code in your onCreate methode: PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);, and the make your activity implement the OnSharedPreferenceChangeListener interface , then call your methode in the callback onSharedPreferenceChanged – user1897934 Aug 01 '13 at 14:10
  • yes, i have the preference screen (called settings.java) and of course the MainActivity..right now the notification code is in the MainActivity. Have i to also put the whole sendSimpleNotificatio() in the settings.java? – David_D Aug 01 '13 at 14:14
  • if you are using the code in the mainActivity try to put it in an utility Class that both your mainActivity and your setting Activity instanciate, but just try to bing the code and see if it works – user1897934 Aug 01 '13 at 14:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34633/discussion-between-user1897934-and-david-d) – user1897934 Aug 01 '13 at 14:19
  • i can't because it's blocked by a proxy in where i work -.- now i update my first post and you can see – David_D Aug 01 '13 at 14:26
  • in the settings activity onCreate add the following: PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceCh‌​angeListener(this); and implement the onSharedPreferenceChanged interface, in the onSharedPreferenceChanged call the cade that you imported from your MainActivity (sendSimpleNotification) – user1897934 Aug 01 '13 at 14:50
  • so i have to call the sendSimpleNotification in the onCreate in settings.java you mean right? – David_D Aug 01 '13 at 14:55