Last active
August 29, 2015 14:13
-
-
Save hitenpratap/d0d26314fce9b59e1370 to your computer and use it in GitHub Desktop.
Validate Intenet Connection in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hprog99; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
public class ValidateInternetConnection { | |
private Context ctx; | |
public ValidateInternetConnection(Context context){ | |
this.ctx = context; | |
} | |
public boolean isConnectingToInternet(){ | |
ConnectivityManager connectivity = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (connectivity != null) | |
{ | |
NetworkInfo[] info = connectivity.getAllNetworkInfo(); | |
if (info != null) | |
for (int i = 0; i < info.length; i++) | |
if (info[i].getState() == NetworkInfo.State.CONNECTED) | |
{ | |
Toast.makeText(context, "Internet is connected", | |
Toast.LENGTH_SHORT).show(); | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment