title: start my android app when usb connect
body: <p>I try to write app which can start when usb connect
  I learn from <a href="https://stackoverflow.com/questions/11445092/starting-my-android-application-automatically-after-connecting-the-usb-cable?answertab=active#tab-top">Starting my android application automatically after connecting the USB cable.</a></p>
  
  <p>my code is</p>
  
  <pre><code>    package com.example.formatsdcard;
      import java.io.File;
  
  
      import android.os.Bundle;
      import android.os.Environment;
      import android.app.Activity;
      import android.util.Log;
      import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
  
      public class MainActivity extends Activity {
  
  public class OnPowerReceiver extends BroadcastReceiver { 
  
      @Override
          public void onReceive(Context context, Intent intent) {
  
              Intent i = new Intent(context, MainActivity.class);
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);   
          }   
      }   
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Button mButton01 = (Button)findViewById(R.id.button1); 
      mButton01.setOnClickListener(new Button.OnClickListener() 
      { 
        public void onClick(View v) 
        {
            wipeMemoryCard();
        }
      });
  </code></pre>
  
  <p>MY Manifest is </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.formatsdcard"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="7"
      android:targetSdkVersion="17" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission   android:name="android.intent.action.ACTION_POWER_CONNECTED"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
  
      &lt;activity
          android:name="com.example.formatsdcard.MainActivity"
          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;/activity&gt;
      &lt;receiver android:name=".OnPowerReceiver"&gt;
             &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED" /&gt;
             &lt;/intent-filter&gt;
                  &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>my app can run after I click it but it will show there're some problem try again later when I connect my usb cable.</p>
  
  <p>How should I change my code to let it work normally.</p>