Hi, make the ConnectionDetector class as follows
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.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)
{
return true;
}
}
return false;
}
}
you can use this class as follows
ConnectionDetector detector= new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = detector.isConnectingToInternet(); // true or false
with this boolean isInternetPresent you can continue further development.
if (isInternetPresent) {
// Internet Connection is Present
} else {
// Internet connection is not present
}
in support of all this, you need to add permission to your manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
source
share