PreludeCập nhật văn bản thông báo, không phải toàn bộ thông báo
Tôi đang cố gắng để thêm một chronometer trên thông báo. Đồng hồ bấm giờ là một dịch vụ. Mỗi thứ hai dòng này được gọi là (tiếp tục chủ đề là một sự "chạy" boolean, timeString là String xây dựng hiển thị thời gian):
NotificationChrono.updateNotification(getApplicationContext(), continueThread,
NOTIF_ID, timeString, "Chronometer", notificationManager);
Đây là lớp 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());
}
}
Vấn đề
Mỗi giây thông báo sẽ bị xóa và được tạo lại, có nghĩa là mỗi giây thông báo sẽ biến mất và xuất hiện lại trong danh sách thông báo.
Điều tôi muốn chỉ cập nhật văn bản TITLE, chứ không phải hoàn toàn tái tạo thông báo mỗi giây. Có thể không?
Nếu câu trả lời của tôi là câu trả lời cho bạn, vui lòng chấp nhận câu trả lời :) – PieterAelse
Tôi không sử dụng câu trả lời của bạn, tôi thực sự không nhớ chính xác những gì tôi đã làm (tôi nghĩ mẹo đó là với setWhen). Nhưng đọc câu trả lời của bạn, có vẻ như một giải pháp tốt hơn, tôi sẽ chấp nhận điều đó! –