'Android 12 sdk 31 Pending Intent

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. 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.

As soon as I updated the target SDK to 31, i can't update the pending intent flag in android studio project coding, app is crashing.

i don't know exactly where to change the pending intent on the code we have tried a lot methods and various place in code but its not working

there is an error in this code MediaNotificationManager.java

public void startNotify(String playbackStatus) {

        Bitmap largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher);

        int icon = R.drawable.ic_round_pause_24_notifi;
        Intent playbackAction = new Intent(service, RadioService.class);
        playbackAction.setAction(RadioService.ACTION_PAUSE);
        PendingIntent action = PendingIntent.getService(service, 1, playbackAction, 0);

        if(playbackStatus.equals(PlayBackStatus.PAUSED)){

            icon = R.drawable.ic_round_play_arrow_24_notifi;
            playbackAction.setAction(RadioService.ACTION_PLAY);
            action = PendingIntent.getService(service, 2, playbackAction, 0);

        }

        Intent stopIntent = new Intent(service, RadioService.class);
        stopIntent.setAction(RadioService.ACTION_STOP);
        PendingIntent stopAction = PendingIntent.getService(service, 3, stopIntent, 0);

        Intent intent = new Intent(service, MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        //PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, 0);
        PendingIntent pendingIntent;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {



            pendingIntent = PendingIntent.getActivity(
                    service,
                    0, intent,
                    PendingIntent.FLAG_IMMUTABLE);
        }
        else
        {
            pendingIntent = PendingIntent.getActivity(
                    service,
                    0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }

I use following implementation in my Gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'org.greenrobot:eventbus:3.0.0'
    implementation 'com.jakewharton:butterknife:10.2.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
    testImplementation 'junit:junit:4.12'

    implementation "androidx.media:media:1.1.0"

    implementation 'com.google.android.exoplayer:exoplayer:2.9.6'

    implementation 'com.yandex.android:mobileads:4.5.0'
    implementation 'com.yandex.android:mobmetricalib:4.1.1'

    implementation 'com.android.support:multidex:1.0.3'

    implementation 'org.jsoup:jsoup:1.11.1'

    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'androidx.work:work-runtime:2.7.1'


}

Help me to fix the issue

this is logcat:

2022-04-22 22:13:13.718 22231-22231/? E/studio.deploy: Could not remove dir '/data/data/com.sashadeafstudio.vestifm/code_cache/.ll/': No such file or directory
2022-04-22 22:13:14.127 2195-2195/com.google.android.googlequicksearchbox E/HwDetectorWithState: a: 3
2022-04-22 22:13:15.577 23190-23190/com.sashadeafstudio.vestifm E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sashadeafstudio.vestifm, PID: 23190
    java.lang.RuntimeException: Unable to create service com.sashadeafstudio.vestifm.player.RadioService: java.lang.IllegalArgumentException: com.sashadeafstudio.vestifm: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    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.
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4500)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: com.sashadeafstudio.vestifm: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    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.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:559)
        at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:530)
        at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:494)
        at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:468)
        at com.sashadeafstudio.vestifm.player.RadioService.onCreate(RadioService.java:163)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4487)
            ... 9 more

new addition logcat problem:

2022-04-23 14:38:15.693 4008-4008/? E/studio.deploy: Could not remove dir '/data/data/com.sashadeafstudio.vestifm/code_cache/.ll/': No such file or directory
2022-04-23 14:38:15.917 2027-2027/com.google.android.googlequicksearchbox E/HwDetectorWithState: a: 3
2022-04-23 14:38:17.144 7705-7705/com.sashadeafstudio.vestifm E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sashadeafstudio.vestifm, PID: 7705
    java.lang.RuntimeException: Unable to create service com.sashadeafstudio.vestifm.player.RadioService: java.lang.SecurityException: listen
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4500)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.SecurityException: listen
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at com.android.internal.telephony.ITelephonyRegistry$Stub$Proxy.listenWithEventList(ITelephonyRegistry.java:1036)
        at android.telephony.TelephonyRegistryManager.listenFromListener(TelephonyRegistryManager.java:250)
        at android.telephony.TelephonyManager.listen(TelephonyManager.java:5999)
        at com.sashadeafstudio.vestifm.player.RadioService.onCreate(RadioService.java:173)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4487)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7839) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.internal.telephony.TelephonyPermissions.enforceCarrierPrivilege(TelephonyPermissions.java:634)
        at com.android.internal.telephony.TelephonyPermissions.checkReadPhoneState(TelephonyPermissions.java:149)
        at com.android.internal.telephony.TelephonyPermissions.checkCallingOrSelfReadPhoneState(TelephonyPermissions.java:94)
        at com.android.server.TelephonyRegistry.checkListenerPermission(TelephonyRegistry.java:3093)
        at com.android.server.TelephonyRegistry.listen(TelephonyRegistry.java:1024)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source