android - Update text of notification, not entire notification -
prelude
i'm trying add together chronometer on notification. chronometer service. every sec line called (continue thread "running" boolean, timestring elaborated string showing time):
notificationchrono.updatenotification(getapplicationcontext(), continuethread, notif_id, timestring, "chronometer", notificationmanager);
this notificationchrono class:
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()); } }
problem
every sec notification deleted , recreated, visually means every sec notification disappears , reappears in notification list.
what want update title text, not recreating notification exclusively every second. possible?
be sure utilize same notificationcompat.builder builder each time creating notification! although first time have set everything, sec time using builder have set value(s) want update. after it's calling notificationmanager.notify(id, builder.build()) did. if utilize same id notification gets updated (important!).
example:
//first time 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()); //second time builder.setcontenttitle(title); notificationmanager.notify(id, builder.build());
but can utilize setuseschronometer method of notificationcompat class. automatically displays chronometer using given timestamp (able set setwhen).
android android-notifications
No comments:
Post a Comment