title: Android start/stop service if USB device is plugged in/removed without Activity
body: <p>I am trying to start a Service on Android as soon as a certain USB device is plugged in. I have already found <a href="https://github.com/follower/android-background-service-usb-accessory" rel="nofollow">this example</a>, which launches an (invisible) activity with an intent-filter, which on the other hand launches the service.</p>
  
  <p>However, this seems to me a little bit hackery and I am trying to do the same with a <code>BroadcastReceiver</code> with the following code:</p>
  
  <pre><code>public class UsbMessageReceiver extends BroadcastReceiver {
      public UsbMessageReceiver() {
          Log.d("UsbMessageReceiver", "Constructor");
      }
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO: This method is called when the BroadcastReceiver is receiving
          // an Intent broadcast.
          Log.d("UsbMessageReceiver", "onReceive");
          throw new UnsupportedOperationException("Not yet implemented");
      }
  }
  </code></pre>
  
  <p>And the manifest:</p>
  
  <pre><code>&lt;receiver
    android:name=".UsbMessageReceiver"
    android:enabled="true"
    android:exported="true" &gt;
    &lt;intent-filter&gt;
        &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
    &lt;/intent-filter&gt;
  
    &lt;meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                  android:resource="@xml/device_filter" /&gt;
  
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Edit: If forgot to mention that this approach does not work. I don't get the log message and the Error is not thrown. Is this possible without invoking an Activity?</p>