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.
yes and also replace
https
withhttp
and no need to HIT page and get response.Just make HEAD
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.).
Use this simple method to check the connectivity
Using this we can check any network connected to Device. You can specifically check either WIFI or GPRS by specifying additionaly
where 0 and 1 respectively refers to mobile and wifi connection
For android >= API LEVEL 23 use: