Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 12 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

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

like image 376
Dark_Clouds_369 Avatar asked Apr 18 '26 08:04

Dark_Clouds_369


2 Answers

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)
    }
}
like image 80
Shahab Saalami Avatar answered Apr 20 '26 20:04

Shahab Saalami


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.

like image 45
David Wasser Avatar answered Apr 20 '26 21:04

David Wasser