【问题标题】:Music Player Foreground service Stops when App is removed from recent apps in Android 12从 Android 12 中的最近应用程序中删除应用程序时,音乐播放器前台服务停止
【发布时间】: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


    【解决方案1】:

    像这样开始你的服务

    Context context = getApplicationContext();   
     Intent intent = new Intent(...); 
    context.startForegroundService(intent);
    

    从前台开始通知

    startForeground(ONGOING_NOTIFICATION_ID, notification);
    

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多