J'ai un service qui crée une notification lorsqu'il est lancé.
Et ensuite ondestroy() je veux qu'il soit supprimé.
J'utilise simplement .cancel(NOTIFICATION_ID) ;
Cela fonctionne très bien lorsqu'il s'agit d'une notification normale, mais lorsque j'utilise un événement en cours, il ne veut pas l'annuler.
J'ai lu quelque chose sur le fait que les services ne s'arrêtent pas si Android a les ressources nécessaires, mais comment surmonter cela ?
J'utilise ce code pour démarrer le service
final Intent bg_service = new Intent(BackgroundService.class.getName());
btn_start = (Button)findViewById(R.id.btn_start);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(bg_service);
}
});
btn_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(bg_service);
}
});
Je l'utilise pour créer
notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A new notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
Intent intent = new Intent(this, mainapp.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "...",
"...", activity);
notificationManager.notify(19314, notification);
Et ceci dans sur détruire
noficationManager.cancel(19314);