Android SetContinuous后无法清除通知(false)
我有一个音乐播放器,在播放音频时显示媒体通知。我希望用户能够在音乐暂停时取消通知 我必须使用startForeground,以便服务将继续运行,并保持与活动的连接。我尝试过使用一个通知通道,一旦我杀死了活动,播放也停止了,这不是我想要的(代码仍然在那里,注释掉了) 当显示通知时,我调用startForeground。SetContinuous设置为playbackState==正在播放的状态。当服务被破坏或playbackState==null | |停止时,我调用stopForeground 巴斯德宾: 代码片段(因为您需要在pastebin中包含代码) PasteBin中的相关方法包括开始通知、停止通知、创建通知、添加播放暂停响应和设置通知播放后台状态使用Android SetContinuous后无法清除通知(false),android,service,notifications,audio-player,android-music-player,Android,Service,Notifications,Audio Player,Android Music Player,我有一个音乐播放器,在播放音频时显示媒体通知。我希望用户能够在音乐暂停时取消通知 我必须使用startForeground,以便服务将继续运行,并保持与活动的连接。我尝试过使用一个通知通道,一旦我杀死了活动,播放也停止了,这不是我想要的(代码仍然在那里,注释掉了) 当显示通知时,我调用startForeground。SetContinuous设置为playbackState==正在播放的状态。当服务被破坏或playbackState==null | |停止时,我调用stopForeground
停止前台(false)
和重新生成通知。在使用前台服务时,您不必使用setconsuming
。有关更多信息,请参阅此
并在前台搜索运行服务的
private void addPlayPauseAction(NotificationCompat.Builder builder) {
String label;
int icon;
PendingIntent intent;
if (playbackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
label = service.getString(R.string.play_pause);
icon = android.R.drawable.ic_media_pause;
intent = intentPause;
builder.setOngoing(true);
} else {
label = service.getString(R.string.play_pause);
icon = android.R.drawable.ic_media_play;
intent = intentPlay;
builder.setOngoing(false);
}
builder.addAction(new NotificationCompat.Action(icon, label, intent));
}