title: detecting usb devices android
body: <p>I am trying to make a toast pop up in my android app when a usb device is attached and detached..<br>
  the short - Its not working..  can someone please tell me what i am missing?  trying to connect a samsung hard keyboard to a samasung tablet and have my app detect it on attach or detach</p>
  
  <p>my activity</p>
  
  <pre><code>    import java.util.ArrayList;
      import java.util.HashMap;
  
      import com.eliddell.R;
      import android.app.Activity;
      import android.app.PendingIntent;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.content.res.Configuration;
      import android.hardware.usb.UsbAccessory;
      import android.hardware.usb.UsbDevice;
      import android.hardware.usb.UsbManager;
      import android.location.GpsStatus.Listener;
      import android.mtp.MtpDevice;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.inputmethod.InputMethodManager;
      import android.widget.Toast;
  
      public class UiModeTestActivity extends Activity {
          /** Called when the activity is first created. */
          //UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              String str = (String) getLastNonConfigurationInstance();
  
              IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
  registerReceiver(mUsbReceiver, filter);
  
          }
  
  
         private static final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";
         private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  
              public void onReceive(Context context, Intent intent) {
                  Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
                  String action = intent.getAction();
                  if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                      UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                      Toast.makeText(getApplicationContext(), "attached", Toast.LENGTH_SHORT).show();
                  }
                  if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
                      UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                      Toast.makeText(getApplicationContext(), "detached", Toast.LENGTH_SHORT).show();
                  }
              }
          };
  
      };
  </code></pre>
  
  <p>my manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-sdk android:minSdkVersion="12" /&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
  
      &gt;
      &lt;uses-library android:name="com.android.future.usb.accessory" /&gt; 
      &lt;activity
          android:name=".UiModeTestActivity"
          android:configChanges="keyboard|uiMode|orientation"
          android:label="@string/app_name" &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;meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
              android:resource="@xml/accessory_filter" /&gt;
      &lt;/activity&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>