getBooleanExtra
Retrieve extended data from the intent.
public boolean getBooleanExtra (String name, boolean defaultValue)
name | String : The name of the desired item.
|
defaultValue |
|
Returns | |
---|---|
boolean | the value of an item previously added with putExtra(), or the default value if none was found. |
如果没有在Intent中找到name所指定的附加数据,则该函数返回defaultValue。
如下:
intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
其中UsbManager.EXTRA_PERMISSION_GRANTED这个附加数据,是系统自动加上的。
通常,在请求USB设备的权限时,会使用
requestPermission(device: UsbDevice!, pi: PendingIntent!)函数来对权限进行申请。
UsbManager manager = (UsbManager) mAndroidContext.getSystemService(Context.USB_SERVICE);
manager.requestPermission(usbDevice, permissionIntent);
requestPermission的调用,Requests temporary permission for the given package to access the device. 请求指定包访问设备的临时权限。
会导致系统显示一个系统对话框(如果该设备还未给当前包授权),提示用户给当前设备授权。用户的选择,又通过pi反馈给系统。也就是说系统会将pi,广播出来。系统在广播出来之前会在pi中增加如下两个键及其数据。
EXTRA_DEVICE
containing the device passed into this callEXTRA_PERMISSION_GRANTED
containing boolean indicating whether permission was granted by the user
因此,如果系统想捕获这个intent,则需要有一个广播接收器来捕获这个intent,并做相应的处理。