I am checking the network status of a device. using:-
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
I want to take this on a little further and check to see if the device can access the internet or a given site such as http://www.google.com and if it can then return as true and if it can't then return as false.
The problem with my code is that it will return true if the device is connected to a router and the router is offline. I need an actual internet connection check.
Amended code
public void GoToStation(View v)
{
try {
InetAddress ina = InetAddress.getByName("http://188.65.176.98/");
if(ina.isReachable(3000)) {
Intent myIntent = new Intent(MainActivity.this, CustomizedListViewStation.class);
startActivityForResult(myIntent, 0);
} else {
Toast.makeText(this, "You need a data connection to view Safety Zones", Toast.LENGTH_LONG).show();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}