public boolean isConnected(Context context) {
connected=false;
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.getState() == NetworkInfo.State.CONNECTED && info.isConnected()) {
Log.e("link",new Gson().toJson(info.getDetailedState()));
connected=true;
Needle.onBackgroundThread().execute(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL(mainDomain).openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
connected=true;
Log.e("connection",new Gson().toJson(urlc.getResponseCode()));
} catch (IOException e) {
e.printStackTrace();
connected=false;
}
}
});
return connected;
}
}
return connected;
}
I have used this code. its working fine, but my issue is that there i need to call server. Is there any other way to check speed or working of WiFi ?
WifiManager wifiManager=(WifiManager)this.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();
I used above code to check speed but it also not working fine.
Please help me out. Thanks in advance
You Can try This
public static boolean checkInternetConnection(Context context)
{
try
{
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null && wifi.isConnected())
|| (mobile != null && mobile.isConnected()))
{
return true;
}
log("Connection", "Connection failed");
return false;
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
return false;
}
}