如何控制锁定屏幕上的通知更新频率
问题描述:
我有一个应用程序通过服务更新通知。如何控制锁定屏幕上的通知更新频率
此通知每秒更新一次。它显示一个计时器。
当前在nougat中,设备锁定,通知更新触发器屏幕醒来,显示更新的通知。
我想控制这一点,因为我的通知真的很频繁。
我也想避免改变通知的更新频率。这样做会向用户显示较不准确的信息。
所以我正在寻找的是一种编程方式来控制通知更新后唤醒屏幕频率。
谢谢:)
答
这下面的通知可以用于频繁更新用户以及当锁定了屏幕不会醒来。您可以尝试
if(notificationBuilder == null) {
notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.extend(wearableExtender)
.setAutoCancel(true)
.setContentTitle("Random Notification")
.setContentText("Appears!")
.setOnlyAlertOnce(true)
.setOngoing(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(contentIntent);
}else{
notificationBuilder.setContentText("change this to current updated text");
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(888, notificationBuilder.build());
+0
谢谢。我尝试过,但不起作用。该通知来自前台服务。所以我用startForeground开始它。我不知道它是否有所作为。你测试了你的解决方案吗? – unludo
+0
雅我每隔一秒发布一个具有相同通知ID并使用相同通知生成器的通知。锁定时电话没有醒来。 –
我不确定您是否可以控制“唤醒屏幕频率”,但为什么不在更新通知前检查屏幕是否处于开启状态?屏幕关闭时更新通知没有意义。检查[这里](https://stackoverflow.com/questions/2474367/how-can-i-tell-if-the-screen-is-on-in-android)检查屏幕是否开启或关闭。 – Swordsman
@Swordsman听起来像个好主意,我会尝试并让你知道。 – unludo