title: usb accessory, catch USB_ACCESSORY_ATTACHED by intent-filter of Service
body: <p>In android usb accessory <a href="http://developer.android.com/guide/topics/usb/accessory.html" rel="nofollow">documentation</a>, there is example of android manifest, where Activity catch USB_ACCESSORY_ATTACHED by intent-filter. Im asking myself, its possible to catch the same intent by intent-filter of Service/IntentService ?</p>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>I tried this,but without success:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.entreprise.ws.main"
      android:versionCode="1"
  
      android:versionName="1.0" &gt;
  
  
      &lt;uses-sdk android:minSdkVersion="10" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
  
          &gt;
              &lt;activity
              android:name="com.entreprise.ws.main.WeatherStationClientActivity"
              android:exported="true"
              android:screenOrientation="portrait"
               &gt;
  &lt;!--              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;  --&gt;
  
          &lt;/activity&gt;
  
          &lt;activity android:name=".EntryPointActivity"
  
              android:screenOrientation="portrait" &gt;
  
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
              &lt;activity
              android:name="com.entreprise.ws.main.WiFiListActivity"
              android:label="@string/appname_wifilist" &gt;
              &lt;/activity&gt; 
  
  
              &lt;uses-library android:name="com.android.future.usb.accessory" /&gt;
  
  
           &lt;activity
              android:screenOrientation="portrait"
              android:name=".WSInstallatorActivity"
              android:exported="true"
               &gt;
  
  
  
          &lt;/activity&gt; 
  
      &lt;service class="services.MyService" android:name="services.MyService"&gt;
                       &lt;intent-filter&gt;
              &lt;action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /&gt;
              &lt;action android:name="android.hardware.usb.action.USB_ACCESSORY_DETACHED" /&gt;
          &lt;/intent-filter&gt;
  
                                  &lt;meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                  android:resource="@xml/accessory_filter" /&gt;
      &lt;/service&gt;
  
  
  
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>service:</p>
  
  <pre><code>package services;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  import android.widget.Toast;
  
  public class MyService extends Service {
  
      final static String TAG = "MyService";
      @Override
      public void onCreate() {
          super.onCreate();
          Log.i("TAG", "onCreate");
          Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.i("TAG", "onDestroy");
          Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
      }
      @Override
      public IBinder onBind(Intent intent) {
          Log.i("TAG", "onBind");
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>
  
  <p>P.S for activity, this works fine</p>
  
  <p><em><strong>EDIT2</em></strong></p>
  
  <p>I experimenting by moving intent-filter and meta-data to my broadcast receiver: I
  not receive no more attach event. What is funny, I continue receive
  detach event.
  Its looks like USB_ACCESSORY_ATTACHED works only with activity,
  despite "Broadcast Action" classification in documentation.</p>
  
  <p><em><strong>EDIT3: Final conclusion</em></strong></p>
  
  <p>Its looks like USB_ACCESSORY_ATTACHED can be caught <em>only</em> in activity (in reason of possible dialog with user ?! ). detach event can be catch in receivers. </p>