1/**
2 * @author Pratik Butani
3 */
4public class InternetConnection {
5
6 /**
7 * CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT
8 */
9 public static boolean checkConnection(Context context) {
10 final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
11
12 if (connMgr != null) {
13 NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
14
15 if (activeNetworkInfo != null) { // connected to the internet
16 // connected to the mobile provider's data plan
17 if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
18 // connected to wifi
19 return true;
20 } else return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
21 }
22 }
23 return false;
24 }
25}
1class NetWorkInfoUtility {
2
3 public boolean isWifiEnable() {
4 return isWifiEnable;
5 }
6
7 public void setIsWifiEnable(boolean isWifiEnable) {
8 this.isWifiEnable = isWifiEnable;
9 }
10
11 public boolean isMobileNetworkAvailable() {
12 return isMobileNetworkAvailable;
13 }
14
15 public void setIsMobileNetworkAvailable(boolean isMobileNetworkAvailable) {
16 this.isMobileNetworkAvailable = isMobileNetworkAvailable;
17 }
18
19 private boolean isWifiEnable = false;
20 private boolean isMobileNetworkAvailable = false;
21
22 public boolean isNetWorkAvailableNow(Context context) {
23 boolean isNetworkAvailable = false;
24
25 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
26
27 setIsWifiEnable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
28 setIsMobileNetworkAvailable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
29
30 if (isWifiEnable() || isMobileNetworkAvailable()) {
31 /*Sometime wifi is connected but service provider never connected to internet
32 so cross check one more time*/
33 if (isOnline())
34 isNetworkAvailable = true;
35 }
36
37 return isNetworkAvailable;
38 }
39
40 public boolean isOnline() {
41 /*Just to check Time delay*/
42 long t = Calendar.getInstance().getTimeInMillis();
43
44 Runtime runtime = Runtime.getRuntime();
45 try {
46 /*Pinging to Google server*/
47 Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
48 int exitValue = ipProcess.waitFor();
49 return (exitValue == 0);
50 } catch (IOException e) {
51 e.printStackTrace();
52 } catch (InterruptedException e) {
53 e.printStackTrace();
54 } finally {
55 long t2 = Calendar.getInstance().getTimeInMillis();
56 Log.i("NetWork check Time", (t2 - t) + "");
57 }
58 return false;
59 }
60}
1new CheckNetworkConnection(this, new CheckNetworkConnection.OnConnectionCallback() {
2
3 @Override
4 public void onConnectionSuccess() {
5 Toast.makeText(context, "onSuccess()", toast.LENGTH_SHORT).show();
6 }
7
8 @Override
9 public void onConnectionFail(String msg) {
10 Toast.makeText(context, "onFail()", toast.LENGTH_SHORT).show();
11 }
12}).execute();
1public class CheckNetworkConnection extends AsyncTask < Void, Void, Boolean > {
2 private OnConnectionCallback onConnectionCallback;
3 private Context context;
4
5 public CheckNetworkConnection(Context con, OnConnectionCallback onConnectionCallback) {
6 super();
7 this.onConnectionCallback = onConnectionCallback;
8 this.context = con;
9 }
10
11 @Override
12 protected void onPreExecute() {
13 super.onPreExecute();
14 }
15
16 @Override
17 protected Boolean doInBackground(Void...params) {
18 if (context == null)
19 return false;
20
21 boolean isConnected = new NetWorkInfoUtility().isNetWorkAvailableNow(context);
22 return isConnected;
23 }
24
25 @Override
26 protected void onPostExecute(Boolean b) {
27 super.onPostExecute(b);
28
29 if (b) {
30 onConnectionCallback.onConnectionSuccess();
31 } else {
32 String msg = "No Internet Connection";
33 if (context == null)
34 msg = "Context is null";
35 onConnectionCallback.onConnectionFail(msg);
36 }
37
38 }
39
40 public interface OnConnectionCallback {
41 void onConnectionSuccess();
42
43 void onConnectionFail(String errorMsg);
44 }
45}