63

前奏曲

通知にクロノメーターを追加しようとしています。クロノメーターはサービスです。毎秒、この行が呼び出されます (継続 Thread は「実行中の」ブール値であり、timeString は時間を示す精巧な文字列です):

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

これは NotificationChrono クラスです。

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

問題

1 秒ごとに通知が削除され、再作成されます。視覚的には、1 秒ごとに通知が消えて通知リストに再表示されることを意味します。

私が望むのは、毎秒完全に通知を再作成するのではなく、TITLE テキストを更新することです。出来ますか?

4

3 に答える 3

130

!NotificationCompat.Builderを作成するたびに必ず同じビルダーを使用してください。Notification

最初はすべてを設定する必要がありますが、2 回目に を使用するBuilderと、更新する値を設定するだけで済みます。その後は、notificationManager.notify(id, builder.build())あなたと同じように呼び出しています。同じものを使用するとID、通知が更新されます (重要!)。

例:

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

ただし、クラスのsetUsesChronometerメソッドを使用することもできます。NotificationCompatこれにより、指定されたタイムスタンプを使用してクロノメーターが自動的に表示されます ( setWhenで設定できます)。

于 2013-05-08T07:48:24.680 に答える