Divyesh Rudani
Divyesh Rudani

Reputation: 249

Solution of intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) return false in Android API level 31 and above

You need to use explicit intent.

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                Intent intent = new Intent(ACTION_USB_PERMISSION);
                intent.setPackage(mContext.getPackageName());
                mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_MUTABLE);
            } else {
                mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT);
            }

The setPackage(mContext.getPackageName()) restricts the intent to your own app.

This makes it an explicit intent, as it targets a specific receiver in the same app.

If setPackage() were removed and no specific component was set, the system would look for any app that could handle ACTION_USB_PERMISSION, making it implicit.

Upvotes: 0

Views: 36

Answers (0)

Related Questions