Using multiple classes in Android app
<p>I would like to listen for connection changes in my Android application and alert the user when a connection to the internet is lost/not precent, i have found this <a href="http://stackoverflow.com/questions/1783117/network-listener-android">Network listener Android</a></p>

<p>But i don't know how to use it when it's in a separate class.</p>

<p>MainActivity.java</p>

<pre><code>package dk.zerone.vuc;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

public final boolean networkCheck() {
    ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
        return true;
    } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {
        return false;
    }
    return false;
}

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    /* Splash screen */
    setContentView(R.layout.splash);

    final WebView webview;
    webview = new WebView(MainActivity.this);
    webview.getSettings().setJavaScriptEnabled(true);

    if(networkCheck()) {

        String url = "http://mobil.vucfyn.dk/mobil";
        webview.loadUrl(url);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) { 
                super.onPageFinished(view, url); 
                setContentView(webview);
             }
        });

    } else {

        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Fejl");
        alertDialog.setMessage("Ingen forbindelse til internettet");

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
         });
        alertDialog.show();
     }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
} 
}
</code></pre>

<p>ConnectionChangeReciever.java</p>

<pre><code>package dk.zerone.vuc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class ConnectionChangeReceiver extends BroadcastReceiver 
{ 
  @Override 
  public void onReceive( Context context, Intent intent ) 
  { 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); 
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE ); 
    if ( activeNetInfo != null ) 
    { 
  Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
  Log.i("mobNetInfo", "Hurray");
} 
if( mobNetInfo != null ) 
{ 
  Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
  Log.i("mobNetInfo", "Hurray");
}
if( mobNetInfo == null ) 
{ 
  Log.i("mobNetInfo", "NULL");
    } 
  } 
}
</code></pre>

<p>AndroidManifest.xml</p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dk.zerone.vuc"
android:versionCode="1"
android:versionName="1.0" &gt;

&lt;uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" 
    android:hardwareAccelerated="true"
    /&gt;

&lt;uses-permission 
    android:name="android.permission.INTERNET" /&gt;

&lt;uses-permission
    android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;

&lt;application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" &gt;
    &lt;activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" &gt;

        &lt;receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver" 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; 

        &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;/application&gt;
</code></pre>

<p></p>
