1

I have a big problem: I want control if 3G or WiFi are activated. This is my code:

//controllo se è accesa la connessione 
        ConnectivityManager cm =
                (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork.isConnectedOrConnecting();
        //controllo se sono connesso
        if(isConnected==false){
            final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Attenzione!");
        builder.setMessage("L'applicazione senza la connessione ad internet non può funzionare. La preghiamo di attivarla.");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setPositiveButton("OK", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();}

It's into the oncreate. When I open the activity I have a force close. The cause is this:

Caused by: java.lang.NullPointerException

I don't understand where is the problem :(

Bishan
  • 15,211
  • 52
  • 164
  • 258
Lorenzo
  • 117
  • 3
  • 9

4 Answers4

2

If there is no active connection, getActiveNetworkInfo() returns null.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

You can Check if the device has Internet connection or not as:

public  boolean CheckConnection() {
    ConnectivityManager cm = (ConnectivityManager) MbridgeApp.getContext().getSystemService(
        Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
      return true;
    }

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
      return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
      return true;
    }
    return false;
  }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none if the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available.

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your android manifest.

K_Anas
  • 31,226
  • 9
  • 68
  • 81
1
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = false;
if(activeNetwork!=null)
    isConnected = true;
activeNetwork.isConnectedOrConnecting();
if(!isConnected){
   final AlertDialog.Builder builder=new AlertDialog.Builder(YourActivity.this);
   builder.setTitle("Attenzione!");
   builder.setMessage("L'applicazione senza la connessione ad internet non può funzionare. La preghiamo di attivarla.");
   builder.setIcon(android.R.drawable.ic_dialog_alert);
   builder.setPositiveButton("OK", new OnClickListener() {

   public void onClick(DialogInterface dialog, int which) {
   }
   });
   builder.show();
}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • nothing to :( force close at line activeNetwork.isConnectedOrConnecting(); caused by a nullpoinete :/ – Lorenzo Jun 02 '12 at 14:54