2

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

Cakston MWS
  • 71
  • 2
  • 12
  • 1
    Welcome to Stackoverflow! Please edit your question and add the code that creates the `PendingIntent`. – David Wasser Jan 30 '22 at 21:54
  • Any one please help – Cakston MWS 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 – Cakston MWS Feb 14 '22 at 05:42

2 Answers2

2

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.M) {
             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.M) {
         return PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_IMMUTABLE | flag);
     } else {
         return PendingIntent.getBroadcast(context, id, intent, flag);
     }
 }
Shahab Saalami
  • 557
  • 6
  • 15
1

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
  • 89,914
  • 16
  • 195
  • 259
  • 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 – Cakston MWS 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) – Cakston MWS 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 – Cakston MWS 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 – Cakston MWS Feb 22 '22 at 03:47
  • 1
    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 – Cakston MWS 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 – Cakston MWS 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
  • 1
    @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