title: Start an Android app from a Broadcast
body: <p>I'm new to the Android API and i try hard to start my app (NOT an Activity) from a Broadcast receiver. It's compiling and manualy launching on my Device, but not automatically launching when i plug an USB device in.</p>
  
  <p>My goal : plug usb in -> start app -> "Hello World!"</p>
  
  <p>===============================[edit]======================================</p>
  
  <p>That's the final version that is working for me. If it's not working for you test with an other device.</p>
  
  <p>Works on my Moto G 1st Gen. Android 5.1.1.</p>
  
  <p>Do not work on my S3 I9300 Android 4.4.4</p>
  
  <p>tests :</p>
  
  <p>Reboot -> OS running -> plug USB in -> unlock the screen -> "Hello world"</p>
  
  <p>Unlock -> plug in -> "Hello world"</p>
  
  <p>...</p>
  
  <p>The code :</p>
  
  <pre><code>package com.example.desktop.helloworld;
  
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class HelloworldActivity extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_helloworld);
      TextView tv = new TextView(this);
      tv.setText("Hello World!");
      setContentView(tv);
  }
  public static class BootUpReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent i = new Intent(context, HelloworldActivity.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(i);
      }
  }
  
  
  }
  </code></pre>
  
  <p>The Manifest :</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.example.desktop.helloworld"&gt;
  &lt;uses-feature android:name="android.hardware.usb.host" /&gt;
  &lt;uses-permission             android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".HelloworldActivity"&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;receiver android:enabled="true" android:name=".HelloworldActivity$BootUpReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&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;
  &lt;/application&gt;
  </code></pre>
  
  <p>
  device_filter (NOT REQUIERED, then comment the reffence in the manifest): </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;usb-device vendor-id="0403" product-id="6001"/&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>Android Studio 2.0,
  OTG Cable,
  Android 4.4.4 .</p>