Android Media3:通过点击媒体通知启动应用程序,但未设置 PendingIntent 的附加内容

问题描述 投票:0回答:1

我想当用户点击媒体通知时将用户直接带到播放器 UI。我看到该应用程序已启动(或带到前台),但意图附加内容为空,我无法将用户带到播放器屏幕。

在 MediaSessionService onCreate() 中我有

      val intent = packageManager.getLaunchIntentForPackage(packageName)
      intent?.putExtra("DESTINATION", "player")
      val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)

      mediaSession = MediaSession.Builder(this, player)
          .setSessionActivity(pendingIntent)
          .build()

在 MainActivity onResume/onStart/onNewIntent 中,我看到意图附加功能未设置。

    override fun onStart() {
        super.onStart()
        Timber.e("onStart: intents: ${intent.extras?.size()}") // <--- this is 0
    }

这就是我所拥有的清单。

      <!-- in app gradle module -->
      <activity
          android:name=".MainActivity"
          android:exported="true">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>

      <!-- in UI gradle module -->
      <service
          android:name="com.scribd.app.ui.service.PlaybackService"
          android:foregroundServiceType="mediaPlayback"
          android:exported="true">
          <intent-filter>
              <action android:name="androidx.media3.session.MediaSessionService"/>
          </intent-filter>
      </service>

我正在 API 级别 34 中对此进行测试。

我错过了什么?另外,我在不同的 gradle 模块中有 MainActivity 和 MediaSessionService,但我认为清单无论如何都会合并。

android-notifications android-mediaplayer exoplayer android-mediasession android-media3
1个回答
0
投票

问题显然出在

packageManager.getLaunchIntentForPackage(packageName)
。虽然它正确地识别了
MainActivity
,但意图额外信息可能会丢失,因为 Activity 位于不同的 gradle 模块中。当我将活动移到与服务相同的模块中时,我开始看到额外的值。不确定根本原因,但至少这是我找到的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.