Skip to content Skip to sidebar Skip to footer

How To Detect Internet Connection

Possible Duplicate: Android detect if device has internet connection I have one program that I allow only android user that has internet connected can access, other user that do

Solution 1:

Here's the code I use in my Android app:

CharSequencenetwork_fail="This application requires that you are connected to the Internet.";
            intduration= Toast.LENGTH_SHORT;
            booleanisAvailable=false;

            // Check availability of network connectiontry
            {
                ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                if(cm == null)
                    isAvailable = false;
                elseisAvailable= cm.getActiveNetworkInfo().isAvailable();
            }
            catch(Exception e){}

Put your Internet-reliant code inside this if:

// Check if user is connected to the Internet, and show an error if they are not.if(isAvailable && (isAirplaneModeOn(context) == false))
            {
              // Internet-reliant code
            }
            else
            {
                Toastfail= Toast.makeText(context, network_fail, duration);
                fail.show();
            }

You also need the obligatory <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in your AndroidManifest.xml file with my code as well.

Solution 2:

//To check whether network connection is available on device or notpublicstaticbooleancheckInternetConnection(Context _activity) {
        ConnectivityManagerconMgr= (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null
                && conMgr.getActiveNetworkInfo().isAvailable()
                && conMgr.getActiveNetworkInfo().isConnected()) 
            returntrue;
        elsereturnfalse;
    }//checkInternetConnection()

AndroidManifest.xml:

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

Solution 3:

You can check if user is connected to internet or not by using ConnectivityManager as:

public boolean checkNetworkStatus() {

    final ConnectivityManager connMgr = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isAvailable()) {
     if (wifiInfo.getState() == NetworkInfo.State.CONNECTED)
      {
        Toast.makeText(this, "Wifi connection.", Toast.LENGTH_LONG).show();
        returntrue;
      }
      else
      {
        Toast.makeText(this, "No Connect using wifi connection.", Toast.LENGTH_LONG).show();
        returnfalse;
      }
    } elseif (mobile.isAvailable()) {
      if (mobile.getState() == NetworkInfo.State.CONNECTED)
      {
        Toast.makeText(this, "Connected using GPRS connection.", Toast.LENGTH_LONG).show();
        returntrue;
      }
      else
      {
        Toast.makeText(this, "No Connect using GPRS connection.", Toast.LENGTH_LONG).show();
        returnfalse;
      }

    } else {
        Toast.makeText(this, "No network connection.", Toast.LENGTH_LONG).show();
        returnfalse;
    }       

}

AndroidManifest.xml:

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

Solution 4:

You should make an BroadcastReceiver that will be triggered when the connectivity status has changed :

publicclassBroadCastSampleActivityextendsActivity {
    /** Called when the activity is first created. */@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver,
                newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
    privateBroadcastReceivermConnReceiver=newBroadcastReceiver() {
        publicvoidonReceive(Context context, Intent intent) {
            booleannoConnectivity= intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            Stringreason= intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            booleanisFailover= intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfocurrentNetworkInfo= (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfootherNetworkInfo= (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            if(currentNetworkInfo.isConnected()){
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

and then in your AndroidManifest you can check if you have connectivity:

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

For reference here here source

Post a Comment for "How To Detect Internet Connection"