Sithira/FlutterRadioPlayer

PendingIntent Flag missing in Android > 31

Closed this issue · 4 comments

gabdy commented

In Android 31 i'm getting an error, that the PendingIntent needs a flag.
I tried to fix it by adding "PendingIntent.FLAG_IMMUTABLE", but i had no luck.
Is there a solution for this problem?

This is the error log:

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:58465/2GRamNiEcaY=/ws
I/javaClass(10003): Calling to method: initService
I/javaClass(10003): start service invoked
I/javaClass(10003): Attempting to initialize service...
I/javaClass(10003): Service not bound, binding now....
I/javaClass(10003): Mapping method call to player item object
I/javaClass(10003): Firing up service. (onStartCommand)...
I/javaClass(10003): LocalBroadCastManager Received...
W/t.studiopiu.vm(10003): Accessing hidden method Landroid/media/AudioTrack;->getLatency()I (unsupported, reflection, allowed)
I/ExoPlayerImpl(10003): Init 66048fd [ExoPlayerLib/2.13.1] [emulator64_arm64, sdk_gphone64_arm64, Google, 31]
I/javaClass(10003): Pushing Event: flutter_radio_stopped
I/javaClass(10003): onPlayerStateChanged: STOPPED
I/javaClass(10003): Pushing Event: flutter_radio_loading
I/javaClass(10003): onPlayerStateChanged: LOADING
D/CompatibilityChangeReporter(10003): Compat change id reported: 160794467; UID 10146; state: ENABLED
D/AndroidRuntime(10003): Shutting down VM
E/AndroidRuntime(10003): FATAL EXCEPTION: main
E/AndroidRuntime(10003): Process: com.example.app, PID: 10003
E/AndroidRuntime(10003): java.lang.RuntimeException: Unable to start service me.sithiramunasinghe.flutter.flutter_radio_player.core.StreamingCore@d40233e with Intent { cmp=com.example.app/me.sithiramunasinghe.flutter.flutter_radio_player.core.StreamingCore (has extras) }: java.lang.IllegalArgumentException: com.example.app: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
E/AndroidRuntime(10003): Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
E/AndroidRuntime(10003): 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4661)
E/AndroidRuntime(10003): 	at android.app.ActivityThread.access$2000(ActivityThread.java:247)
E/AndroidRuntime(10003): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2095)
E/AndroidRuntime(10003): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(10003): 	at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime(10003): 	at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime(10003): 	at android.app.ActivityThread.main(ActivityThread.java:7842)
E/AndroidRuntime(10003): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(10003): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime(10003): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
E/AndroidRuntime(10003): Caused by: java.lang.IllegalArgumentException: com.example.app: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
E/AndroidRuntime(10003): Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
E/AndroidRuntime(10003): 	at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
E/AndroidRuntime(10003): 	at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
E/AndroidRuntime(10003): 	at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
E/AndroidRuntime(10003): 	at com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1395)
E/AndroidRuntime(10003): 	at com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1351)
E/AndroidRuntime(10003): 	at com.google.android.exoplayer2.ui.PlayerNotificationManager.<init>(PlayerNotificationManager.java:658)
E/AndroidRuntime(10003): 	at com.google.android.exoplayer2.ui.PlayerNotificationManager.<init>(PlayerNotificationManager.java:568)
E/AndroidRuntime(10003): 	at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:521)
E/AndroidRuntime(10003): 	at me.sithiramunasinghe.flutter.flutter_radio_player.core.StreamingCore.onStartCommand(StreamingCore.kt:248)
E/AndroidRuntime(10003): 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4643)
E/AndroidRuntime(10003): 	... 9 more
I/Process (10003): Sending signal. PID: 10003 SIG: 9
Lost connection to device.
Exited (sigterm)

I tried this, but had no luck with it:
(android/src/main/kotlin/me/sithiramunasinghe/flutter/flutter_radio_player/core/StreamingCore)

override fun createCurrentContentIntent(player: Player): PendingIntent {
 var intent = Intent(this@StreamingCore, activity!!.javaClass)
 var contentPendingIntent = PendingIntent.getActivity(this@StreamingCore, 0, intent, PendingIntent.FLAG_IMMUTABLE );
   return contentPendingIntent;
   }

Device: Pixel 4 API 31 - Android 12.0

[✓] Flutter (Channel beta, 2.9.0-0.1.pre, on macOS 12.1 21C52 darwin-arm, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.63.2)
[✓] Connected device (3 available)

I could make it work by adding the required PendingIntent code and upgrading exoplayer to 2.14.2, if anyone still need it.

I could make it work by adding the required PendingIntent code and upgrading exoplayer to 2.14.2, if anyone still need it.

please show me how you did it

justinbeatz can you guide to solve this issue

Has been updated to v2. Please check the master branch