I'm checking Internet connection state by pinging google. The problem is, when there is no connection and the waiting time is hyper-extended.
This is my code:
private boolean checkInternet() {
String netAddress = null;
try
{
netAddress = new NetTask().execute("www.google.com").get();
return (!netAddress.equals(""));
}
catch (Exception e1)
{
e1.printStackTrace();
return false;
}
return false;
}
public class NetTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
InetAddress addr = null;
try
{
addr = InetAddress.getByName(params[0]);
}
catch (UnknownHostException e)
{
e.printStackTrace();
return "";
} catch (IOException time)
{
time.printStackTrace();
return "";
}
return addr.getHostAddress();
}
}
I can not concatenate isReachable(int timeout)
because it returns a boolean
. How can I solve that?