- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个媒体服务,它使用 startForeground() 在播放开始时显示通知。播放时有暂停/停止按钮,暂停时有播放/停止按钮。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
// setup...
Notification n = mBuilder.build();
if (state == State.Playing) {
startForeground(mId, n);
}
else {
stopForeground(false);
mNotificationManager.notify(mId, n);
}
这里的问题是当我显示/更新处于暂停状态的通知时,应该允许您将其删除。 mBuilder.setOngoing(false)
似乎没有效果,因为前面的 startForeground
覆盖了它。
使用相同的代码调用 stopForeground(true);
可以按预期工作,但通知会在销毁和重新创建时闪烁。有没有办法“更新”从 startForeground 创建的通知以允许在调用 stop 后将其删除?
编辑:根据要求,这里是创建通知的完整代码。每当播放或暂停服务时都会调用 createNotification。
private void createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("No Agenda")
.setContentText("Live stream");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
if (state == State.Playing) {
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pausePendingIntent = PendingIntent.getService(MusicService.this, 0, pauseIntent, 0);
mBuilder.addAction(R.drawable.pause, "Pause", pausePendingIntent);
//mBuilder.setOngoing(true);
}
else if (state == State.Paused) {
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pausePendingIntent = PendingIntent.getService(MusicService.this, 0, pauseIntent, 0);
mBuilder.addAction(R.drawable.play, "Play", pausePendingIntent);
mBuilder.setOngoing(false);
}
Intent stopIntent = new Intent(this, MusicService.class);
stopIntent.setAction(ACTION_STOP);
PendingIntent stopPendingIntent = PendingIntent.getService(MusicService.this, 0, stopIntent, 0);
setNotificationPendingIntent(mBuilder);
mBuilder.addAction(R.drawable.stop, "Stop", stopPendingIntent);
}
else
{
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0, resultIntent, 0);
mBuilder.setContentIntent(intent);
}
Notification n = mBuilder.build();
if (state == State.Playing) {
startForeground(mId, n);
}
else {
stopForeground(true);
mNotificationManager.notify(mId, n);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setNotificationPendingIntent(NotificationCompat.Builder mBuilder) {
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
}
后续编辑:
下面的评论之一提到答案可能是“脆弱的”,并且随着 Android 4.3 的发布,startForeground
背后的行为发生了变化。 startForeground 将强制您的应用程序在前台显示通知,并且应该只调用该方法并显示通知。我尚未测试,但接受的答案可能不再按预期工作。
在调用 stopForeground
时停止闪烁方面,我认为不值得为框架而战。
有一些 additional information on the Android 4.3 notification change here .
最佳答案
您可以考虑使用不同的方法。
由于您应该为此类任务(媒体播放)使用前台服务,我建议您继续执行 start foreground()
,但不要将通知传递给它,只需设置 id 0 和通知 null 像这样 startForeground(0, null);
.
这样前台服务不会显示任何通知。
现在,出于您的目的,您可以使用常规通知并更新它们的状态(持续、布局、文本等),这样您就不必依赖前台服务的通知行为。
希望这会有所帮助。
关于android - 允许在调用 stopForeground(false) 后取消通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610399/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!