Search code examples
javaandroidflutterkotlinandroid-pendingintent

Requirement of using PendingIntent.FLAG_IMMUTABLE when requesting USB Permission


I'm trying to give permissions to an connected USB Device using PendingIntent. However when using PendingIntent.FLAG_MUTABLE, i get following warning:

Mutable implicit PendingIntent will throw an exception, follow either of these recommendations: for an existing PendingIntent use FLAG_NO_CREATE and for a new PendingIntent either make it immutable or make the Intent within explicit

But if i use PendingIntent.FLAG_IMMUTABLE, it will never allow me to get permissions for the attached USB Device

What should i do now?

The code i use now:

 val permissionIntent = PendingIntent.getBroadcast(context, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)
 mManager?.requestPermission(device, permissionIntent)

Solution

  • Use PendingIntent.FLAG_IMMUTABLE instead.

    FYI check this: USBMonitor#register

    Update 1 ------

    Just use PendingIntent.FLAG_MUTABLE and set package to your intent.
    Like this answer: https://stackoverflow.com/a/77691737/23272559