Mise à jour du texte de la notification, et non pas toute la notification

Prélude

J'essaye d'ajouter un chronomètre sur la notification. Le chronomètre est un service. Chaque seconde de cette ligne est appelée (continuer Thread est un "running" boolean, timeString est élaboré Chaîne de caractères indiquant le temps):

NotificationChrono.updateNotification(getApplicationContext(), continueThread, 
NOTIF_ID, timeString, "Chronometer", notificationManager);

C'est le NotificationChrono classe:

public class NotificationChrono {
static public void updateNotification(Context context, boolean running,
int id, String title, String text,
NotificationManager notificationManager) {
Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,
0, stopIntent, 0);
Intent startIntent = new Intent(
"com.corsalini.david.barcalc.STARTPAUSE");
PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,
0, startIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10, new Intent(
context, FrontActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0))
.addAction(
running ? R.drawable.ic_action_pause
: R.drawable.ic_action_play,
running ? context.getString(R.string.pause) : context
.getString(R.string.start), startPendingIntent)
.addAction(R.drawable.ic_action_stop,
context.getString(R.string.stop), stopPendingIntent);
notificationManager.notify(id, builder.build());
}
}

Problème

Chaque seconde la notification est supprimé et recréé, visuellement cela signifie que chaque seconde de la notification disparaît et réapparaît dans la liste de notification.

Ce que je veux, c'est juste à jour le TITRE du texte, pas de recréer la notification entièrement à chaque seconde. Est-il possible?

  • Si ma réponse n'a le truc pour vous, veuillez accepter, c'est comme la réponse 🙂
  • Je n'ai pas votre réponse, en fait je ne me souviens pas exactement de ce que j'ai fait (je pense que le truc était avec setWhen). Mais la lecture de votre réponse, il semble une meilleure solution, je vais accepter ça!