按照教程https://developer.android.com/guide/topics/connectivity/usb/host.html编写应用程序文本。插入usb device
时,我的应用会自动运行。但我不想要它。如何在插入usb device
时阻止我的应用自动运行?
以下是我的广播接收器:
public class UsbPermissionReceiver extends BroadcastReceiver {
private static final String TAG = "UsbPermissionReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (AppConstant.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) {
Log.w(TAG, "============");
}
} else {
Log.w(TAG, "permission denied for device " + device);
}
}
}
}
}
在activity
registerReceiver(mPermissionReceiver, filter);
但我觉得这个接收器永远不会运行!
然后我尝试枚举设备以连接USB设备:
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.android.example.USB_PERMISSION"), 0);
for (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
if (usbDevice.getVendorId() == 8263
&& usbDevice.getProductId() == 768) {
if (mUsbManager.hasPermission(usbDevice)) {
Log.i("===", "=======================");
startService(new Intent(AppConstant.SERVICE_ACTION));
} else {
Log.i("===", "00000000000000000000000");
mUsbManager.requestPermission(usbDevice, mPermissionIntent);
}
}
}
我delete
manifest config
。因此我的应用无法在usb device
插入时自动发布。但也有一个问题:如果我没有插入usb device
应用不会再次请求许可并且沟通良好,否则它会要求许可再次。但我检查了默认值。我真的不明白,有人帮助我。