Hola gente..sto cercando di creare una notifica attivabile o meno attraverso una preferences screen. Quindi, se clicco nel checkboxpreferences la notifica partirà, contrario se tolgo il check sparirà. Il codice finora creato è questo:
codice:
private void sendSimpleNotification(){
boolean permanent = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("permanent", false);
if(permanent) {
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);
}
}
Se questo codice lo lascio così com'è non succede nulla..posso cliccare nel checkboxpreferences mille volte ma non succede nulla. Se per esempio invece richiamo sendSimpleNotification(); nell'onResume, succede che nel momento in cui esco dal menù delle impostazioni la notifica parte e uguale se la voglio togliere. Detto questo so che il codice funziona..ora, quello che però voglio io è che la notifica parta subito nel momento in cui faccio il check nel checkbox. Non devo uscire dalle preferences perchè parta o sparisca..capito?Devo richiamare sendSimpleNotification(); in qualche altro punto?