允许在调用stopForeground后取消通知(false)

Wil*_*ins 17 android android-service android-notifications android-notification-bar

我有一个Media服务,它使用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);        
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是当我显示/更新处于暂停状态的通知时,应该允许您将其删除.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);
}
Run Code Online (Sandbox Code Playgroud)

后续编辑:

下面的评论之一提到答案可能是"脆弱的",并且自Android 4.3发布以来,背后的行为startForeground已经改变.startForeground将强制您的应用程序在前台显示通知,并且应该通过显示通知来调用该方法.我没有测试过,但接受的答案可能不再符合预期.

在呼叫时停止闪烁方面stopForeground,我认为不值得为这个框架而战.

这里有关于Android 4.3通知更改的一些其他信息.

Ino*_*man 6

您可以考虑使用不同的方法.
既然你应该使用前台服务来完成这样的任务(媒体播放),我建议你继续这样做start foreground(),但是不要向它发送通知,只需设置id 0和通知null就像这样startForeground(0, null);.

这样前台服务将不会显示任何通知.

现在,出于您的目的,您可以使用常规通知并更新其状态(正在进行的,布局,文本等...),这样您就不依赖于前台服务的通知行为.

希望这可以帮助.

  • 似乎很脆弱.我觉得谷歌希望有一个关于前台服务的通知,所以我不会指望当前的行为在未来的Android版本中工作......我建议使用stopForeground(true)并使用通知flash. (2认同)

aks*_*692 6

而不是stopService(true),调用stopService(false)将按原样保留通知(没有正在进行的状态),除非它被用户解除/以编程方式删除或服务停止。因此,只需调用 stopService(false) 并更新通知以显示暂停状态,现在用户可以关闭通知。这也可以防止闪烁,因为我们没有重新创建通知。