我已经获得此许可:
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
将文件保存到本地是可以的。但是我无法保存到USB
存储设备。
如何获得另一个写许可?
答案 0 :(得分:0)
UsbManager.requestPermission()
可以与BroadcastReceiver
一起使用:
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
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 = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null) {
//call method to set up device communication
}
} else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
必须注册:
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
然后可以请求权限:
UsbDevice device;
mUsbManager.requestPermission(device, mPermissionIntent);
有关如何转移-以及如何releaseInterface()
和close()
的信息,请参见USB Host。