private boolean CheckInternet() {
ConnectivityManager connec = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
return true;
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("No Internet Connectivity ! ").setCancelable(
false).setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
}
Using this Function u can check in device the internet connection is available or not.
ReplyDeleteprivate boolean CheckInternet() {
ReplyDeleteConnectivityManager connec = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()) {
return true;
} else if (!mobile.isConnected()) {
Toast.makeText(getApplicationContext(),"No Internet Connection",5000).show();
return false;
} else {
new AsyncCheckInternet().execute();
}
return false;
}
private class AsyncCheckInternet extends AsyncTask {
@Override
protected void onPreExecute() {
dialogProcess = ProgressDialog.show(Classname.this, "",
"Checking Internet Connection...", true);
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean params) {
dialogProcess.dismiss();
if(params == false)
{
Toast.makeText(getApplicationContext(),"No Internet Connection",5000).show();
finish();
}
else
new AsyncLoadXMLFeed().execute();
}
}