broadcastreceiver - Solution of intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) return false in Android API l

You need to use explicit intent.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {Intent intent =

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745165653a4614610.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信