Check The Internet Connectivity In Android
I am trying to pull as well as push some data to and from the server via webservice. The mandatory thing that should i have to do is connectivity check. What i have done right now
Solution 1:
You can register a BroadcastReceiver
to listen for connectivity changes. A detailed post can be found here.
Solution 2:
Hi i do these way maybe there better
privatebooleancheckInternetConnection() {
ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connectionif (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
returntrue;
} else {
//no conectionreturnfalse;
}
}
Solution 3:
publicstaticbooleanisInternetAvailable(Context context){
ConnectivityManagerconnec= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfowifi= connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfomobile= connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isConnected() || mobile.isConnected()){
// Check for web site try{
// Create a URL for the desired pageURLurl=newURL("http://www.google.com");
// Read all the text returned by the serverBufferedReaderin=newBufferedReader(newInputStreamReader(url.openStream()));
in.close();
returntrue;
} catch (Exception e) {
returnfalse;
}
}
returnfalse;
}
The method also checks whether a certain website in this case the www.google.com is available. This might be useful as the device might be connected to a WLAN router which has no internet access. In this case wifi.isConnected()
would also return true
although no internet is available.
Solution 4:
for check internet connection in android..
publicstaticbooleanisOnline(Activity act)
{
ConnectivityManagercm= (ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetInfo= cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
{
returntrue;
}
returnfalse;
}
Post a Comment for "Check The Internet Connectivity In Android"