Check Gprs Connection

4.2k views Asked by At

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 isOnline(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL("https://telemeter4tools.services.telenet.be/TelemeterService");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "My Android Demo");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds

urlc.connect();

if (urlc.getResponseCode() == 200) {
return true;
} else {
return false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

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

5

There are 5 answers

0
Jigar Joshi On

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.

public static boolean isGPRSWorking(String URL){
    try {
      HttpURLConnection.setFollowRedirects(false);

      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }  
0
Rohit Sharma On

Use this simple method to check the connectivity

 public boolean isOnline()
{

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo ni = cm.getActiveNetworkInfo();
     boolean result = 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

 ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
  // ARE WE CONNECTED TO THE NET
if ( 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

0
Al Sutton On

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.).

0
Hare-Krishna On
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();
        return true;

    } else if (mobile.isAvailable()) {
        Toast.makeText(this, "GPRS connection.", Toast.LENGTH_LONG).show();
        return true;

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

}
0
Ton Snoei On

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

For android >= API LEVEL 23 use:

public boolean isMobileDataNetworkConnected(Context context) {

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

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

        int networkType = networkInfo.getType();

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

            return true;
        }
    }
    return false;
}