通知更新文字,而不是整个通知 [英] Update text of notification, not entire notification

查看:188
本文介绍了通知更新文字,而不是整个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

prelude

我想加一个计时表上的通知。该天文台是一个服务。每一秒这条线被称为(继续线程是在运行布尔,TimeString以是详尽的字符串显示时间):

I'm trying to add a chronometer on the notification. The chronometer is a service. Every second this line is called (continue Thread is a the "running" boolean, timeString is the elaborated String showing the time):

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

这是NotificationChrono类:

This is the 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());
}
}

问题

每秒钟通知被删除并重新创建,在视觉上就意味着每一秒钟通知消失,重新出现在通知列表中显示。

Every second the notification is deleted and recreated, visually it means that every second the notification disappears and reappears in the notification list.

我想是刚刚更新的标题文本,而不是重新创建通知完全每一秒。这可能吗?

What I would want is to just update the TITLE text, not recreating the notification entirely every second. Is it possible?

推荐答案

请务必每次都使用相同的NotificationCompat.Builder制造商创建的通知! 虽然第一次你必须设置的一切,用Builder中的第二次,你只需要设置你想要更新的值(S)。之后,它的调用notificationManager.notify(ID,builder.build()),就像你一样。如果你用相同的ID,然后通知被更新(重要!)。

Be sure to use the same NotificationCompat.Builder builder each time for creating the Notification! Although the first time you have to set everything, the second time using the Builder you only have to set the value(s) you want to update. After that it's calling notificationManager.notify(id, builder.build()) just like you did. If you use the same ID then the notification gets updated (important!).

例如:

//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());

但你也可以使用<一个href="http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setUsesChronometer%28boolean%29">setUsesChronometer在NotificationCompat类的方法。这会自动显示使用给定的时间戳(可设置<一个天文台表href="http://developer.android.com/reference/android/app/Notification.Builder.html#setWhen%28long%29">setWhen).

But you can also use the setUsesChronometer method of the NotificationCompat class. This automatically displays a chronometer using the given timestamp (able to set with setWhen).

这篇关于通知更新文字,而不是整个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆