【发布时间】:2022-07-07 23:38:00
【问题描述】:
我正在使用这个例子开发一个简单的音乐播放器
https://www.youtube.com/watch?v=svdq1BWl4r8
当应用从 Android 12 中的最近应用中删除时,音乐播放器服务停止
我使用的代码是
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
private void notification_manager() {
playerNotificationManager = new PlayerNotificationManager.Builder(AudioPlayerService.this, ConstantValues.PLAYBACK_CHANNEL_ID_INT_ID,
ConstantValues.PLAYBACK_CHANNEL_ID)
.setMediaDescriptionAdapter(mediaDescriptionAdapter)
.setNotificationListener(notificationListener)
.setCustomActionReceiver(null).setChannelNameResourceId(R.string.app_name).setChannelImportance(NotificationUtil.IMPORTANCE_HIGH)
.build();
//Sets how PlayerNotification will Look
playerNotificationManager.setPriority(NotificationCompat.PRIORITY_HIGH);
playerNotificationManager.setPlayer(player);
playerNotificationManager.setUseFastForwardAction(false);
playerNotificationManager.setUseRewindAction(false);
playerNotificationManager.setUseRewindActionInCompactView(false);
playerNotificationManager.setUseChronometer(false);
playerNotificationManager.setUsePlayPauseActions(true);
playerNotificationManager.setUseStopAction(true);
playerNotificationManager.setColorized(true);
playerNotificationManager.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE);
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(AudioPlayerService.this, MainActivity.class);
PendingIntent pendingIntent = null;
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.S){
pendingIntent = PendingIntent.getActivity(AudioPlayerService.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(AudioPlayerService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
return pendingIntent;
}
当我们从最近的应用程序中删除应用程序时,它会在 Android 11 及更高版本上停止,但在较低版本上运行良好
gradle
compileSdk 31
defaultConfig {
applicationId "com.test.musicplayer"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
【问题讨论】:
标签: android notifications android-12