如何实时更新Android通知?

时间:2016-04-15 13:45:07

标签: android

我已经看到一些应用程序实时更新其通知内容。一个例子是倒计时器应用程序。

enter image description here

单击+ ADD 1 MIN按钮会立即更改通知文本。这是怎么做到的?或者换句话说,当用户点击按钮时,如何实时更新通知?

enter image description here

4 个答案:

答案 0 :(得分:3)

我通常将NotificationCompat.Builder声明为成员变量。

然后,您可以使用该构建器在将来更新通知。

private NotificationCompat.Builder mBuilder = null;

public void refreshNotifications() {
    if(mBuilder == null) {
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setAutoCancel(false);
        mBuilder.setOngoing(true);
        mBuilder.setOnlyAlertOnce(true);
        mBuilder.setVisibility(Notification.VISIBILITY_PRIVATE);
        mBuilder.setSmallIcon(R.drawable.ic_launcher_notification_outline);
        mBuilder.setOngoing(true);
        mBuilder.setContentTitle(context.getString(R.string.downloading_file));
    }

    mBuilder.setContentText(context.getString(R.string.total_progress, percentProgress));
    notificationManager.notify(DOWNLOADING_FILE, mBuilder.build());
}

答案 1 :(得分:1)

你看过这个吗?来自android docs ......

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("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
    mNotifyBuilder.setContentText(currentText)
        .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

答案 2 :(得分:1)

为了测试,我做了一个倒数计时器来更新通知

CountDownTimer timer = new CountDownTimer(30000, 1000)
    {
        public void onTick(long millisUntilFinished)
        {
            refreshNotifications("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish()
        {
        }
    }.start();

更新@Assaf之前告知的通知方法

public void refreshNotifications(String message) {
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.mipmap.ic_launcher);
    int numMessages = 0;
    // Start of a loop that processes data and then notifies the user
    mBuilder.setContentText(message)
            .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mBuilder.build());
}

要进行自定义布局,您可以阅读此文档http://developer.android.com/intl/pt-br/guide/topics/ui/notifiers/notifications.html#CustomNotification

答案 3 :(得分:-1)

在文本视图中设置计时器。每当按下“+ ADD 1 min”时。用旧计时器+ 1分钟更新计时器。