10

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

I can't update the pending intent flag in android studio project coding

This is a place in AlarmPingSender.java where the error occurred

  public void start()        
   {       
   String action = MqttServiceConstants.PING_SENDER
            + comms.getClient().getClientId();
    Log.d(TAG, "Register alarmreceiver to MqttService"+ action);
    service.registerReceiver(alarmReceiver, new IntentFilter(action));

    pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
            action), PendingIntent.FLAG_UPDATE_CURRENT);

    schedule(comms.getKeepAlive());
    hasStarted = true;
}

Help me to fix the issue ERROR IN ANDROID STUDIO IMAGE

Dark_Clouds_369
  • 658
  • 1
  • 6
  • 24
  • Any one please help – Dark_Clouds_369 Feb 12 '22 at 07:27
  • The error message tells you pretty much exactly what to do. Also there are numerous questions on Stackoverflow covering this exact problem. See https://developer.android.com/guide/components/intents-filters#DeclareMutabilityPendingIntent – David Wasser Feb 13 '22 at 19:26
  • 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 – Dark_Clouds_369 Feb 14 '22 at 05:42

4 Answers4

7

Use this two public methods when you want to create any PendingIntent in your project

Create activity pendingIntent

   public static PendingIntent createPendingIntentGetActivity(Context context, int id, Intent intent, int flag) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
             return PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_IMMUTABLE | flag);
         } else {
             return PendingIntent.getActivity(context, id, intent, flag);
         }
     }

Create broadcast pendingIntent

 public static PendingIntent createPendingIntentGetBroadCast(Context context, int id, Intent intent, int flag) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
         return PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_IMMUTABLE | flag);
     } else {
         return PendingIntent.getBroadcast(context, id, intent, flag);
     }
 }

Kotlin answer:

fun getActivity(context: Context?, id: Int, intent: Intent?, flag: Int): PendingIntent {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_MUTABLE or flag)
    } else {
        PendingIntent.getActivity(context, id, intent, flag)
    }
}

fun getBroadcast(context: Context?, id: Int, intent: Intent?, flag: Int): PendingIntent {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getBroadcast(context, id, intent!!, PendingIntent.FLAG_MUTABLE or flag)
    } else {
        PendingIntent.getBroadcast(context, id, intent!!, flag)
    }
}
Shahab Saalami
  • 862
  • 10
  • 18
6

You need to do this:

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
        action), PendingIntent.FLAG_UPDATE_CURRENT |
                 PendingIntent.FLAG_IMMUTABLE);

Since you are using AlarmManager you should be able to use the IMMUTABLE flag.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • sir can i know the exact place we need to use this code , As we have tried this already its not working for us . We worked in AlarmPingIntent to change this – Dark_Clouds_369 Feb 15 '22 at 11:33
  • [App Coding](https://drive.google.com/file/d/121gVQJASqxXPrFb2KfJoZfbB-tzK_1rO/view?usp=sharing) The link contains coding of the demo application which we created based on the exact concept we used kindly help in this @[David Wasser](https://stackoverflow.com/users/769265/david-wasser) – Dark_Clouds_369 Feb 16 '22 at 07:37
  • 1
    Have you seen this? Are you using the same MQTT library? https://stackoverflow.com/questions/71155187/android-paho-mqtt-crashes-android-12-targeting-s-version-31-and-above-requi – David Wasser Feb 21 '22 at 09:58
  • Will work and let u know sir i think its as same as what we got – Dark_Clouds_369 Feb 21 '22 at 11:53
  • 1
    There is a link to another version of the library in that ticket that may solve your problem. – David Wasser Feb 21 '22 at 15:04
  • ok sir lets try it – Dark_Clouds_369 Feb 22 '22 at 03:47
  • 2
    Were you able to fix your problem with the linked solution? – David Wasser Feb 25 '22 at 09:48
  • yes sir but it is in kotlin format we use java – Dark_Clouds_369 Feb 26 '22 at 06:06
  • What is in Kotlin format? What did you do to fix the problem? – David Wasser Feb 26 '22 at 12:45
  • sir can u give us a step by step procedure for the fixing of the problem – Dark_Clouds_369 Feb 28 '22 at 03:40
  • @David Wasser Can you expand on your comment to "...probably need to use the MUTABLE flag." with Alarm Manager? I use AlarmManager to fire alarms to trigger Notifications for user due dates and am wondering if I should be using MUTABLE flag. – AJW Apr 05 '22 at 20:17
  • @AJW Reviewing my comment, I assumed that `AlarmManager` was adding something to the `Intent` when it was triggered, which would imply that `FLAG_MUTABLE` was necessary. However, I just looked again and `AlarmManager` doesn't add anything to the `Intent`, so you should be safe using `FLAG_IMMUTABLE`. Sorry for the confusion. I've edited my answer accordingly. – David Wasser Apr 06 '22 at 09:09
  • @David Wasser No apology necessary, thanks for reviewing and clarifying. I did see in Android Developer guide that MUTABLE can be used if you have a repeating alarm, to allow the EXTRA_ALARM_COUNT intent extra but only for that use case: https://developer.android.com/guide/components/intents-filters#DeclareMutabilityPendingIntent – AJW Apr 06 '22 at 15:03
  • 2
    @AJW Generally speaking you need to use `FLAG_MUTABLE` if you give the `PendingIntent` to another app and that app needs to add/modify the `Intent` when it ultimately triggers the `PendingIntent`. – David Wasser Apr 06 '22 at 17:35
3

I had the same issue, and adding JUST the IMMUTABLE flag to my PendingIntents did not work for some reason.

Please make sure that in app/build.gradle, you have at least the 2.7.1 work manager version (If you're also using WorkManager inside the app):

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

I had 1.0.3. After upgrading to this (or latest version), the problem was solved.

Gabriel Trifa
  • 173
  • 11
3

If you are not using PendingIntent anywhere in your code but still get this error, chances are that one of your library dependencies does so just update your dependencies to their latest versions (its what worked for me)

m'hd semps
  • 564
  • 4
  • 14