我在做一个uvc摄像头功能,在android10.0系统下可以正常请求权限并使用,但是在android10.0系统下直接收到权限请求失败,并没有弹出权限请求提示弹窗,我还尝试将意图过滤器和元数据添加到清单文件的活动标记中。插入USB时,我直接选择了相关应用并授予权限,但是USB摄像头仍然无法正常工作,代码如下
<!-- AndroidManifrest.xml-->
<uses-feature android:name="android.hardware.usb.host" />
<!-- Activity-->
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
//定义广播标识
private static final String ACTION_USB_PERMISSION = "com.tangtang.uvctest.USB_PERMISSION";
//注册接收广播 获取到usb 权限时系统广播
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
//成功
toastMsg("permission succ for device " + device);
}
} else {
//The code will execute directly when requesting permissions
toastMsg("permission denied for device " + device);
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},0);
}
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPermission1();
}
});
}
private void requestPermission1() {
//获取service
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
//获取设备列表(一般只有一个,usb 口只有一个)
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
//绑定广播
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
//注册接收广播
registerReceiver(mUsbReceiver, filter);
//枚举设备
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
//请求权限
manager.requestPermission(device, mPermissionIntent);
}
}
private void toastMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mUsbReceiver);
}
}