序幕
我正在尝试在通知上添加计时器.计时器是一项服务.每一行调用此行(继续Thread是一个"running"布尔值,timeString是精心设计的String,显示时间):
NotificationChrono.updateNotification(getApplicationContext(), continueThread,
NOTIF_ID, timeString, "Chronometer", notificationManager);
Run Code Online (Sandbox Code Playgroud)
这是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
: …Run Code Online (Sandbox Code Playgroud) 我有一个通知,每隔三秒刷新一次(即发送).我已经设置了FLAG_ONGOING_EVENT标志和FLAG_NO_CLEAR标志,以便始终显示.问题是,如果例如下载处于活动状态(在通知区域中显示进度条),则两个通知都会不断切换位置,因为它们每隔几秒刷新一次.
如何将我的通知固定到列表顶部(或某个静态位置),以便每次通过调用更新它时它都会停止跳转NotificationManager.notify()?
编辑:这是更新通知的代码.它每三秒钟运行一次.
Notification notification = new Notification();
notification.contentView = appBarNotification; // this sets the changed notification content
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.icon = R.drawable.icon;
nm.notify(APP_BAR_NOTIFICATION, notification);
Run Code Online (Sandbox Code Playgroud)