android - 从最近使用的应用程序中删除的应用程序取消了前台通知
android - foreground notification is cancelled on app removed from recent apps
我正在制作音乐播放器应用程序,我为音乐播放过程启动了前台服务。但是当应用程序从最近的应用程序中删除时,前台通知被取消并且音乐仍在播放! ,这意味着前台服务没有通知。
我试过了:setAutoCancel(false)
和 setOnGoing(true)
在创建 NotificationCompat.Builder
实例时,两者都不起作用。
我的通知创建代码
public class MediaStyleHelper {
public static final String CHANNEL_ID = "2229";
public static NotificationCompat.Builder from(
Context context, MediaSessionCompat mediaSession) {
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context , CHANNEL_ID);
builder
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setContentIntent(controller.getSessionActivity())
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
.setAutoCancel(false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
return builder;
}
}
清单中的服务:
<service android:name=".services.AudioPlayerService"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
并开始前台代码
private void showPlayingNotification() {
NotificationCompat.Builder builder = MediaStyleHelper.from(this, mMediaSessionCompat);
if( builder == null ) {
return;
}
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setShowActionsInCompactView(0).setMediaSession(mMediaSessionCompat.getSessionToken()));
builder.setSmallIcon(R.drawable.ic_play_circle_outline);
builder.setOngoing(true);
startForeground(1,builder.build());
//NotificationManagerCompat.from(AudioPlayerService.this).notify(1, builder.build());
}
请注意,我使用以下代码创建了通知渠道:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.setSound(null,null);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
解决方案是从 activity 调用 startService(Intent)
。即使我调用 MediaBrowserCompat.connect()
并且媒体播放正确,我也需要调用 startService(Intent)
!
此编辑后的代码是:
public void initMediaBrowserCompat(){
startService(new Intent(this,AudioPlayerService.class));
mMediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, AudioPlayerService.class),
mMediaBrowserCompatConnectionCallback, getIntent().getExtras());
mMediaBrowserCompat.connect();
}
我正在制作音乐播放器应用程序,我为音乐播放过程启动了前台服务。但是当应用程序从最近的应用程序中删除时,前台通知被取消并且音乐仍在播放! ,这意味着前台服务没有通知。
我试过了:setAutoCancel(false)
和 setOnGoing(true)
在创建 NotificationCompat.Builder
实例时,两者都不起作用。
我的通知创建代码
public class MediaStyleHelper {
public static final String CHANNEL_ID = "2229";
public static NotificationCompat.Builder from(
Context context, MediaSessionCompat mediaSession) {
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context , CHANNEL_ID);
builder
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setContentIntent(controller.getSessionActivity())
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
.setAutoCancel(false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
return builder;
}
}
清单中的服务:
<service android:name=".services.AudioPlayerService"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
并开始前台代码
private void showPlayingNotification() {
NotificationCompat.Builder builder = MediaStyleHelper.from(this, mMediaSessionCompat);
if( builder == null ) {
return;
}
builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setShowActionsInCompactView(0).setMediaSession(mMediaSessionCompat.getSessionToken()));
builder.setSmallIcon(R.drawable.ic_play_circle_outline);
builder.setOngoing(true);
startForeground(1,builder.build());
//NotificationManagerCompat.from(AudioPlayerService.this).notify(1, builder.build());
}
请注意,我使用以下代码创建了通知渠道:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.setSound(null,null);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
解决方案是从 activity 调用 startService(Intent)
。即使我调用 MediaBrowserCompat.connect()
并且媒体播放正确,我也需要调用 startService(Intent)
!
此编辑后的代码是:
public void initMediaBrowserCompat(){
startService(new Intent(this,AudioPlayerService.class));
mMediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, AudioPlayerService.class),
mMediaBrowserCompatConnectionCallback, getIntent().getExtras());
mMediaBrowserCompat.connect();
}