简体   繁体   中英

Android: continuously update text in the notification area

As Richard said in this question > Possible to continuously update text in the notification area? , I have the same issue here .
I want to update text in the notification area .
This question is answered and accepted , but the answer doesn't help me .
It's about to create text as bitmap dynamically and set it to Small Icon of notification .
But as Richard commented , the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly.

Please kindly show me the right way to do this stuff .

It's quite simple to update your Notification. First of all when you create it you must create it with an id.

NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("Title")
    .setContentText("Text")
    .setSmallIcon(R.drawable.ic_notify);
mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

Now to update your Notification you only need to notify the new notification with the old id and this will be updated (you don't need to reset the paraneter you don't want to update).

  mNotifyBuilder.setContentText(setContentText("New Text")
  mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
    //this update your notification
    mNotificationManager.notify(
                notifyID,
                mNotifyBuilder.build());

I tried to make one but it has one disadvantage is that, it has one empty notification in notification drawer,

public static int when = 0;
private void generateNotification(Context context) {
    Log.i(TAG, "generateNotification");
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
            this)
            // .setContentTitle("AppName")
            // .setContentText("Message For Notification Drawer")
            // .setSound(soundUri)
            // .setDefaults(Notification.DEFAULT_SOUND)
            // .setVibrate(new long[] { 1000, 1000 })
            // .addAction(R.drawable.ic_launcher, "View", pIntent)
            // .addAction(0, "Remind", pIntent)
            // .setNumber(fCount)
            // .setWhen(when)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(Integer.toString(when)) // Set String you want
            .setAutoCancel(true)
            .setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // notificationManager.notify(when, mNotification.build());
    notificationManager.notify(1, mNotification.build());
    when++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM