Android: how to launch my app upon receiving a specific Broadcast?
<p>I am trying to get my app automatically launched when the phone gets connected to wifi. Here's the code I am using to both set the Broadcast receiver and to specify that once the broadcast is received I want the "Connected" activity to be launched:</p>

<pre><code>IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
receiver = new BroadcastReceiver(){
                public void onReceive(Context context, Intent intent){
                    Intent intent2 = new Intent(getApplicationContext(),com.example.package.Connected.class);
                    startActivity(intent2);
                }
            };
registerReceiver(receiver,intentFilter); 
</code></pre>

<p>Unfortunately it's not working. The logcat says my activity has "leaked IntentReceiver".</p>

<p>Does anyone know how I can solve this?</p>

<p><strong>EDIT</strong>: I also tried to register the receiver via the Manifest file. I added this code to the manifest:</p>

<pre><code>&lt;receiver android:name="com.example.package.receiver"&gt;
    &lt;intent-filter&gt;
          &lt;action android:name="android.net.wifi.STATE_CHANGE" /&gt;
    &lt;/intent-filter&gt;
&lt;/receiver&gt;
</code></pre>

<p>And then this code to my main activity:</p>

<pre><code>private BroadcastReceiver receiver = new BroadcastReceiver(){
        public void onReceive(Context context, Intent intent){
            Intent intent2 = new Intent(getApplicationContext(),com.example.package.Connected.class);
            context.startActivity(intent2);
        }
    };
</code></pre>

<p>But now my app crashes once the phone connects to wifi. Logcat says "RuntimeException: Unable to instantiate receiver".</p>

<p>Any ideas how to solve it?</p>
