8

我的应用程序没有捕获“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>
4

5 回答 5

9

一切都像上面描述的那样工作,除了一点点变化!在我的研究中,应用程序标签中的 singleTask 没有帮助,但是当我把它放在活动标签中时有帮助

<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);
于 2013-07-16T09:49:56.423 回答
3

也添加此权限:)

<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
于 2013-03-02T09:21:31.043 回答
2

也尝试添加<application android:launchMode="singleTask">. 这对我有用USB_ACCESSORY_ATTACHED,也可能对你有用。

于 2013-03-08T16:56:02.753 回答
1

我能够通过以下方式解决此问题:

  1. 在 Launcher Activity 中添加android:launchMode="singleTask"属性
  2. 在您的启动器活动中添加此意图过滤器

    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
    
  3. 将元数据添加到您的 Launcher Activity

    <meta-data
        android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" 
    />
    
  4. 在启动器活动中覆盖 OnNewIntent 方法

    protected void onNewIntent(Intent intent) {
        if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)){
            getPermission();
        }
    };
    
    1. 现在,如果您要分离 -> 再次附加 USB 设备,onNewIntent()将调用方法..
于 2013-04-13T06:35:10.347 回答
0

Android Reference for Activity 说使用 onNewIntent():

“对于在其包中将 launchMode 设置为“singleTop”的活动,或者如果客户端在调用 startActivity(intent) 时使用了 FLAG_ACTIVITY_SINGLE_TOP 标志,则调用此方法。

那么launchMode singleTop == singleTask?

于 2013-11-29T09:48:44.137 回答