【发布时间】:2011-04-17 14:10:13
【问题描述】:
您好,我如何创建像第一个电池指示器一样的永久通知?
【问题讨论】:
标签: android notifications
您好,我如何创建像第一个电池指示器一样的永久通知?
【问题讨论】:
标签: android notifications
如果你使用NotificationCompat.Builder,你可以使用:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
mBuilder.setOngoing(true); //this will make ongoing notification
【讨论】:
将Notification.FLAG_ONGOING_EVENT 标志分配给您的Notification。
示例代码:
yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...
如果您不熟悉 Notification API,请阅读 Android 开发者网站上的 Creating Status Bar Notifications。
【讨论】:
Service.startForeground而不是NotificationManager.notify()开始持续的通知
|=,而不是=吗?
实际上建议按位或通知标志,而不是直接设置标志。这允许您一次设置多个标志。
例如:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
将同时设置两个标志,而:
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
只会设置 FLAG_SHOW_LIGHTS 标志。
【讨论】:
public static final int FLAG_ONGOING_EVENT
自:API 级别 1
如果此通知涉及正在进行的事情(例如电话),则将按位或位到标志字段中应设置的位。
public static final int FLAG_FOREGROUND_SERVICE
自:API 级别 5 如果此通知表示当前正在运行的服务,则将按位或位到标志字段中。
【讨论】: