How To Check The Internet Connectivity Within The Network In Android (using Internet Of Some Other Device Through Hotspot)
I have a requirement where I want to check whether there is any internet connectivity when I am connected with the network. For example, I have device A and device B. Device A is c
Solution 1:
It can be a ridiculous solution but i think also it could be real solution:
publicbooleanisOnline() {
ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetInfo= cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URLurl=newURL("http://www.google.com");
HttpURLConnectionurlc= (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
returnnewBoolean(true);
}
} catch (MalformedURLException mue) {
// TODO Auto-generated catch block
mue.printStackTrace();
} catch (IOException ie) {
// TODO Auto-generated catch block
ie.printStackTrace();
}
}
returnfalse;
}
Solution 2:
Try the below function to check your internet connection:
publicstaticbooleanisInternetConnected(Context mContext) {
try {
ConnectivityManagerconnect=null;
connect = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connect != null) {
NetworkInforesultMobile= connect
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInforesultWifi= connect
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if ((resultMobile != null && resultMobile
.isConnectedOrConnecting())
|| (resultWifi != null && resultWifi
.isConnectedOrConnecting())) {
returntrue;
} else {
returnfalse;
}
}
} catch (Exception e) {
e.printStackTrace();
}
returnfalse;
}
Add the following permissions to your manifest file,
<uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />
Post a Comment for "How To Check The Internet Connectivity Within The Network In Android (using Internet Of Some Other Device Through Hotspot)"