我的应用程序没有抓住" ACTION_USB_DEVICE_ATTACHED"但是" ACTION_USB_DEVICE_DETACHED"工作得很好。
如果我连接usb设备,应用程序就会正确启动,但在执行期间我会断开连接BroadcastReceiver
右键捕获" ACTION_USB_DEVICE_DETACHED"。如果我再次附上BroadcastReceiver
,请不要抓住任何内容。
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):
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL -->
</resources>
答案 0 :(得分:8)
除了变化之外,一切都如上所述。在我的研究中,singleTask in application标签没有帮助,但是当我把它放在活动标签
时有所帮助<activity
...
android:launchMode="singleTask">
它为我提供了ANDROID开始向活动发送onNewIntent。在onNewIntent上,我只需从意图中获取设备,没有任何权限请求:
@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);
答案 1 :(得分:3)
另外添加此权限:)
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
答案 2 :(得分:2)
还可以尝试添加<application android:launchMode="singleTask">
。 USB_ACCESSORY_ATTACHED
对我有用,也适用于你。
答案 3 :(得分:1)
我能够通过以下方式解决此问题:
android:launchMode="singleTask"
属性
在启动器活动中添加此意图过滤器
<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"
/>
覆盖Launcher活动中的OnNewIntent方法
protected void onNewIntent(Intent intent) {
if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)){
getPermission();
}
};
onNewIntent()
方法将被调用.. 答案 4 :(得分:0)
Android Reference for Activity说使用onNewIntent():
“这是为在其包中将launchMode设置为”singleTop“的活动调用的,或者在调用startActivity(intent)时客户端使用FLAG_ACTIVITY_SINGLE_TOP标志”
所以launchMode singleTop == singleTask?