Android:BroadcastReceiver将设备USB插头插入/拔出USB端口时
我的应用程序没有捕获“ACTION\u USB\u DEVICE\u ATTACHED”但“ACTION\u USB\u DEVICE\u Distached”工作正常。 如果我连接usb设备但在执行过程中断开连接,应用程序将正确启动Android:BroadcastReceiver将设备USB插头插入/拔出USB端口时,android,usb,broadcastreceiver,host,Android,Usb,Broadcastreceiver,Host,我的应用程序没有捕获“ACTION\u USB\u DEVICE\u ATTACHED”但“ACTION\u USB\u DEVICE\u Distached”工作正常。 如果我连接usb设备但在执行过程中断开连接,应用程序将正确启动BroadcastReceiverright catch“ACTION\u usb\u device\u disconnected”。如果我再次连接广播接收器,请不要捕获任何内容 IntentFilter filter = new IntentFilter(
BroadcastReceiver
right catch“ACTION\u usb\u device\u disconnected”。如果我再次连接广播接收器,请不要捕获任何内容
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
rocrailService.registerReceiver(mUsbReceiver, filter);
// BroadcastReceiver when insert/remove the device USB plug into/from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
mSerial.usbAttached(intent);
mSerial.begin(mBaudrate);
loadDefaultSettingValues();
Run=true;
start();
System.out.println("BroadcastReceiver USB Connected");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
mSerial.usbDetached(intent);
mSerial.end();
Run=false;
System.out.println("BroadcastReceiver USB Disconnected");
}
}
并表明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.DCCWLocoDisplay"
android:versionCode="0"
android:versionName="0" >
<uses-sdk android:targetSdkVersion="12"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:icon="@drawable/dccw"
android:label="@string/app_name" >
<activity android:icon="@drawable/cab_64" android:name="net.DCCWLocoDisplay.activities.ActRCCab" android:theme="@style/FullHeightMainDialog" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
<activity android:name="net.DCCWLocoDisplay.activities.ActPreferences" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAccessory" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAbout" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActProgramming" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActSteps" android:theme="@style/FullHeightDialog" />
<service android:name="net.DCCWLocoDisplay.DCCWLocoDisplay"/>
</application>
</manifest>
设备筛选器(我检查供应商和产品id):
同时添加此权限:)
也尝试添加
。这适用于连接了USB附件的我,可能也适用于您。我可以通过以下方式解决此问题:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter"
/>
protected void onNewIntent(Intent intent) {
if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)){
getPermission();
}
};
onNewIntent()
方法一切都像上面描述的那样工作,除了一些小的变化!在我的研究中,应用程序标签中的singleTask并没有帮助,但当我将它放在活动标签中时,它起到了帮助作用
<activity
...
android:launchMode="singleTask">
活动的Android参考说明用于使用onNewIntent(): 对于在其包中将launchMode设置为“singleTop”的活动,或者如果客户端在调用startActivity(intent)时使用了标志\u ACTIVITY\u SINGLE\u TOP标志,则会调用此命令
因此,launchMode singleTop==singleTask?答案如下:。在这两种情况下,只创建了一个实例(与“singleTask”略有不同)。所以launchMode singleTop=singleTask在这里的例子中…设备过滤器文件片段在哪里
protected void onNewIntent(Intent intent) {
if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)){
getPermission();
}
};
<activity
...
android:launchMode="singleTask">
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
{
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);