UsbHost中的requestPermission

时间:2018-02-12 12:18:01

标签: java android usb android-pendingintent

我要求用户授予与连接的USB设备建立连接的权限,但我不了解如何获取结果代码表明成功或失败。使用hasPermission没有帮助。我请求连接的活动如下,

MainActivity.java

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb);
        Button mCheckForDevice = (Button) findViewById(R.id.check);
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        mCheckForDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
                Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
                if(deviceList.size() > 0){
                    UsbDevice device = deviceIterator.next();
                    mUsbManager.requestPermission(device, mPermissionIntent);
    // here I need to print the device info when user accept the permission by clicking on OK button,
    // if CANCEL it has to show a toast with message permission denied by user

                }}
        });
    }

requestPermission是一个返回void的方法。文件说,

Requests temporary permission for the given package to access the device.
     * This may result in a system dialog being displayed to the user
     * if permission had not already been granted.
     * Success or failure is returned via the {@link android.app.PendingIntent} pi.
     * If successful, this grants the caller permission to access the device only
     * until the device is disconnected.

有人可以帮忙查找如何通过PendingIntent返回成功或失败信息?

0 个答案:

没有答案