Android Check Internet Connection using Broadcast
<p>I would like to implement a broadcast receiver for my internet connection checking. If connection is not present just finish(); it. But i am still messing up the context . Please check on my below codes. </p>

<pre><code>/**
 * This broadcast receiver is awoken after boot and registers the service that
 * checks for new photos from all the known contacts.
 */


public class ConnectionDetector extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {



    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);

     if(noConnectivity){

         ((Activity)context).finish();
         //Show Warning Message
         //Close Application the way i suggested
     }

    }


}
</code></pre>

<p>AndroidManifest </p>

<pre><code> &lt;receiver      android:name=".ConnectionDetector"
                        android:label="NetworkConnection"&gt;
        &lt;intent-filter&gt;
            &lt;action     android:name="android.net.conn.CONNECTIVITY_CHANGE"/&gt;
        &lt;/intent-filter&gt;
        &lt;/receiver&gt;
</code></pre>

<p>LogCat :</p>

<pre><code>11-01 22:40:29.179: E/AndroidRuntime(29531): FATAL EXCEPTION: main
11-01 22:40:29.179: E/AndroidRuntime(29531): java.lang.RuntimeException: Unable to start receiver in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.access$2400(ActivityThread.java:117)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.os.Looper.loop(Looper.java:130)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at java.lang.reflect.Method.invoke(Method.java:507)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at dalvik.system.NativeStart.main(Native Method)
11-01 22:40:29.179: E/AndroidRuntime(29531): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
11-01 22:40:29.179: E/AndroidRuntime(29531):    at in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector.onReceive(ConnectionDetector.java:29)
11-01 22:40:29.179: E/AndroidRuntime(29531):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
11-01 22:40:29.179: E/AndroidRuntime(29531):    ... 10 more
</code></pre>
