MediaSessionService wurde zerstört, nachdem die App aus den letzten Apps entfernt wurdeAndroid

Forum für diejenigen, die für Android programmieren
Guest
 MediaSessionService wurde zerstört, nachdem die App aus den letzten Apps entfernt wurde

Post by Guest »

Ich starte den Dienst in onCreate in der Aktivität mit startForegroundService für Android Oreo und höher und startService für niedrigere. Im Dienst habe ich startForeground im Dienst verwendet, indem ich Hilfsmethoden für Benachrichtigungen aufrufe. Wenn die App im Vordergrund ist, funktioniert der Player perfekt und zeigt eine Benachrichtigung an, aber sobald ich die App aus den letzten Apps entfernt habe, wurde der Dienst zerstört. Der Dienst sollte weiterhin als Vordergrunddienst funktionieren, aber ich weiß nicht, warum er immer wieder stoppt.
Ich habe einige Lösungen wie startService anstelle von startForegroundService für Oreo und höher ausprobiert. Ich habe auch andere Lösungen ausprobiert, zuletzt Neustart des Dienstes, nachdem er zerstört wurde, aber er funktioniert, ist aber nicht ideal.
Ich teste die App in Anroid 13

Hier ist mein Aktivität

Code: Select all

class MainActivity : ComponentActivity() {
private var isServiceRunning: Boolean = false

private fun startingService(){
if (!isServiceRunning){
var intent = Intent(this, PlaybackSessionService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
startForegroundService(intent)
}else{
startService(intent)
}

isServiceRunning = true
}
}

@OptIn(UnstableApi::class)
override fun onCreate(savedInstanceState: Bundle?) {

startingService()

//the rest code compose Ui
Hier ist mein PlaybackSessionService

Code: Select all

class PlaybackSessionService: MediaSessionService() {
val mediaSession: MediaSession by inject()
val musiCoNotificationManager: MusiCoNotificationManager by inject()

@OptIn(UnstableApi::class)
override fun onCreate() {
super.onCreate()

Log.e("ks","onCreate service")

musiCoNotificationManager.startNotificationService(
mediaSession = mediaSession,
mediaSessionService = this
)
}

@OptIn(UnstableApi::class)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.e("ks","onStartCommand service")

musiCoNotificationManager.startNotificationService(
mediaSession = mediaSession,
mediaSessionService = this
)

return super.onStartCommand(intent, flags, startId)
}

override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
return mediaSession
}

override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
}

override fun onDestroy() {
super.onDestroy()

Log.e("ks","Service destroyed")
mediaSession.apply {
release()
if (player.playbackState != Player.STATE_IDLE){
player.seekTo(0)
player.playWhenReady = false
player.stop()
player.release()
}
}
}
}
Hier ist mein NotificationManager

Code: Select all

class MusiCoNotificationManager(
private val context: Context,
private val player: ExoPlayer,
): KoinComponent {

@UnstableApi
fun startNotificationService(
mediaSessionService: MediaSessionService,
mediaSession: MediaSession
){

buildNotification(mediaSession)
startForegroundNotificationService(mediaSessionService)
}

private fun startForegroundNotificationService(mediaSessionService: MediaSessionService){
Log.e("ks","Enter startForeground fun in MusiCoNotificationManager class ")
val notification = NotificationCompat.Builder(context, MusicoApp.NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setOnlyAlertOnce(true)
.build()
mediaSessionService.startForeground(MusicoApp.NOTIFICATION_ID,notification)

}

@UnstableApi
private fun buildNotification(mediaSession:  MediaSession){

PlayerNotificationManager.Builder(
context,
MusicoApp.NOTIFICATION_ID,
MusicoApp.NOTIFICATION_CHANNEL_ID
)
.setMediaDescriptionAdapter(
MusiCoNotificationAdapter(context = context, pendingIntent = mediaSession.sessionActivity)
)
.setSmallIconResourceId(R.drawable.music_note)
.build()
.also {
it.setMediaSessionToken(mediaSession.platformToken)
it.setUseFastForwardActionInCompactView(true)
it.setUseNextActionInCompactView(true)
it.setUseRewindActionInCompactView(true)
it.setUsePreviousActionInCompactView(true)
it.setPriority(NotificationCompat.PRIORITY_DEFAULT)
it.setPlayer(player)
}
}

}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post