Thursday, 17 May 2012

Check Internet connection in android application

To check Internet connection is Ok or not? or which type of connection you are using i.e WiFi or GPRS etc...following is a code for that....njoy....and call this method in main onCreate() method.



void checkInternetConnectionStatus() {
ConnectivityManager connMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);


android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


if (wifi.isAvailable()) {
Toast.makeText(this, "Wi-Fi connection", Toast.LENGTH_LONG).show();
} else if (mobile.isAvailable()) {
Toast.makeText(this, "Mobile Internet", Toast.LENGTH_LONG).show();


} else {
/*
* Toast.makeText(this, "No Internet Connection", Toast.LENGTH_LONG)
* .show();
*/
new AlertDialog.Builder(this)
.setTitle("No internet connection active")
.setMessage(
"Please start internet connection and run this application.")
.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.d("AlertDialog", "Negative");
finish();
}
}).show();
}
}

1 comment:

Daniel Lopez said...

I still in the error "could not send the email" solution ???