2013-03-02 169 views
4

我的應用程序不捕獲「ACTION_USB_DEVICE_ATTACHED」,但「ACTION_USB_DEVICE_DETACHED」正常工作。 如果我連接USB設備,但我在執行期間斷開BroadcastReceiver正確的捕獲「ACTION_USB_DEVICE_DETACHED」應用程序正確啓動。如果我再附上BroadcastReceiver什麼也別抓。Android:BroadcastReceiver插入/移除設備USB插入/從USB端口

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> 

回答

8

一切正常,如上所述,除了一點變化!我的研究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); 
2

添加此權限也:)

<uses-feature android:name="android.hardware.usb.host" /> 
<uses-permission android:name="android.permission.USB_PERMISSION" /> 
2

也可以嘗試加入<application android:launchMode="singleTask">。這對USB_ACCESSORY_ATTACHED適合我,也可能適合你。

1

我能夠解決此問題:

  1. 添加android:launchMode="singleTask"屬性中啓動活動
  2. 添加這個意圖過濾器在啓動活動

    <intent-filter> 
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
    </intent-filter> 
    
  3. 添加元數據到你的啓動程序活動

    <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(); 
        } 
    }; 
    
  5. 現在,如果你將脫離 - >再次連接USB設備,onNewIntent()方法將被調用..
0

爲活動的Android參考指出使用onNewIntent():

「這就是所謂的爲設置launchMode活動到他們包中的「singleTop」,或者如果客戶在調用startActivity(intent)時使用了FLAG_ACTIVITY_SINGLE_TOP標誌「

So so launchMode singleTop == singleTask?

+0

答案在這裏找到:[link](http://smartandroidians.blogspot.de/2010/04/activity-launch-mode-in-android.html)。在這兩種情況下,只有一個實例被創建('singleTask'有一點區別)。所以在這裏launchMode singleTop = singleTask這裏... – user1480734 2013-11-29 09:55:36

相關問題