Skip to content Skip to sidebar Skip to footer

Check Gprs Connection

I want to check whether the gprs connection is active or not in android through code to show how can i check that. i have the following code. Will it work? public static boolean i

Solution 1:

Check below code:

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()) {
        Toast.makeText(this, "Wifi connection.", Toast.LENGTH_LONG).show();
        returntrue;

    } elseif (mobile.isAvailable()) {
        Toast.makeText(this, "GPRS connection.", Toast.LENGTH_LONG).show();
        returntrue;

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

}

Solution 2:

can i replace that site with the google and check the response.

yes and also replace https with http and no need to HIT page and get response.

Just make HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

publicstaticbooleanisGPRSWorking(String URL){
    try {
      HttpURLConnection.setFollowRedirects(false);

      HttpURLConnection con =
         (HttpURLConnection) newURL(URLName).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       returnfalse;
    }
  }  

Solution 3:

Use this simple method to check the connectivity

publicbooleanisOnline()
{

     ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfoni= cm.getActiveNetworkInfo();
     booleanresult=false;
     if(ni != null )
     {
         if(  ni.getState() == NetworkInfo.State.CONNECTED )
         {
             result = true;
         }
     }

     return result;


} 

Using this we can check any network connected to Device. You can specifically check either WIFI or GPRS by specifying additionaly

ConnectivityManagerconnec= (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
  // ARE WE CONNECTED TO THE NETif ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) 

where 0 and 1 respectively refers to mobile and wifi connection

Solution 4:

If you're testing for a mobile connection you can see if;

NetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE

on the object returned from cm.getActiveNetworkInfo() in your code. This will return true if the connection is a mobile one.

Testing for GPRS specifically doesn't really make sense because not all mobile devices support the GPRS protocol, and many can use other cellular protocols (CDMA, HSDPA, etc.).

Solution 5:

getNetworkInfo(ConnectivityManager.TYPE_WIFI) was deprecated in API level 23

For android >= API LEVEL 23 use:

publicbooleanisMobileDataNetworkConnected(Context context) {

    ConnectivityManagerconnectivityManager= (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);

    for (Network network : connectivityManager.getAllNetworks()) {
        NetworkInfonetworkInfo= connectivityManager.getNetworkInfo(network);

        intnetworkType= networkInfo.getType();

        if (networkType == ConnectivityManager.TYPE_MOBILE || 
            networkType == ConnectivityManager.TYPE_MOBILE_DUN &&
            networkInfo.isConnected()) {

            returntrue;
        }
    }
    returnfalse;
}

Post a Comment for "Check Gprs Connection"