Access network availability state android - android

I am developing an application where i want to access whether there is internet connectivity or not. I can access the network state by using
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
But this just returns whether the mobile is connected to internet or not. I want to know if there is internet availability after connecting. Like, may be the server is down or internet not available. Please let me know how to accomplish this!

To check that it's a working Internet connection and not just you are connected to Internet. You can do that by trying to fetch a known address/resource from your site, like a 1x1 PNG image or 1-byte text file. Also it will answer you about your server status. :)

Checking internet connection normally implemented, but if you want know your server working running or no, first you need send ping or send stub request(Example: send request with any params) to your server. If have response yes then you can work perfectly with your server.

Try like this,
URL url = new URL("Your URL");
URLConnection conexion = url.openConnection();
conexion.setConnectTimeout(10000); // Don't forget to put a time limit
conexion.connect();
After timeout (10 secs for above mentioned example), It will through time out exception.
So You can use it to check whether Internet access is available or not.

Related

how to check if a device has a "real" internet connection on main thread?

I am using this code to check the connection in my app :
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
this method will return "true" even the device just connect to wifi and not has a connection to the internet or the device turn on mobile network but run out of data. I have read many posts, but most of them are too old. I dont know those answers are still right or not. So, how to check if the device has a "real" connection to the internet ?
This maybe a similar question : Android: Check network and real internet connectivity

Wifi connection works inspite of access point

I have a code that tests if a wifi connection is available.
public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000);
urlc.setReadTimeout(4000);
urlc.connect();
if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.i(TAG,"Internet connection is OK");
return true;
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
Log.i(TAG, "No internet connection.");
return false;
}
To access the internet, I have to through an access point, this is why I espacially ping google because I need if we are also logged in. I have noticed that in many cases, eventhough I do not log into the captive portal, the code sill reaches google and returns true. I am also able to internet servers, such as ftp.
Does anyone know what is the cause of this behavior ? Has anyone else noticed this behavior ?
Thank you,
Some captive portals allow access without logging in to a limited set of services (e.g. company corporative website of the owner of the WiFi service). I've seen Google front page allowed in some WiFi APs I've used over the years. That may be what you've noticed.
Maybe a better approach (or additional) would be to check network detailed state NetworkInfo.html#getDetailedState() which also gives you information about captive portal detection.
Also, checking a specific page in your own servers may help (is less likely to be allowed without logging in), plus doing it through HTTPS instead of plain HTTP may give you additional information (is quite common to get a certificate not matching error because captive portals act like a man-in-the-middle).

Android is device connected to the internet method

I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}

How can I check if Internet connection is turned on every n seconds?

Hi guys I'm new on android and I wish to check every "some" seconds if the internet connection has been turned on from the user.
This is my situation: I've a map on a fragment and I download from my app an xml file with the position of markers, titles, addresses, and many other things. Now if the person that download my app don't have the internet turned on, I can't place any markers on the map. So a popup come out that request to activate an internet connection. But if the person activate it without refresh the main activity nothing happens.
So how can I do it? Which methods I have to use?
There's no need to schedule an update based on an Internet resource if you aren't connected to the Internet. The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
For Determining and Monitoring the Connectivity Status Help Link

Android: HttpClient parameters - Connection and Socket Timeout

I could't find any helpful tutorials on internet nor the documentation on developers site.
In my application i am connecting to a Web Server using HttpPost, when there is no internet connection, but wifi is on it shows a white screen and after some 10-15 secs "UnknownHostException".
I caught this Exception and made toast like
Unable to connect, check your internet connection.
and close the Activity (or the Application, since i am using finish() on the 1st Activity).
When the wifi itself is off i get an instant toast like"
You need internet connection to use this Application
but the 1st case is irritating. Taking 10-15 secs time and then showing the toast.
So i used HttpParameters and added a 5 sec ConnectionTimeout parameter.
But the application works same as before(no effect of this parameters).
How can i track if i hit ConnectionTime(5 secs over). So that i can show a Toast like
Slow internet connection
moreover why is the internet connection check not working when wifi is on but no internet
this is what i check when my application is lauched:
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (isOnline(cm, this, SignUpActivity.this)){
//continue
}
public static boolean isOnline(ConnectivityManager cm, Context c, Activity a) {
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
Toast.makeText(c, "You need internet access to run this application",
Toast.LENGTH_SHORT).show();
a.finish();
return false;
}
am i only checking whether device's wifi is on. if so, how can i check whether i have internet connection, instead of just wifi
Thank You
As for the first question, try using SocketTimeout instead.
As for the second question, the line
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Will only get Wi-fi status (i.e. phone wifi antenna turned on) but not actual connectivity. The function returns immediately, so that if wifi is turned off you can toast out without checking connection further. But when wi-fi is turned on, you should go on and check your server's actual reachability, with something like
InetAddress.getByName(host).isReachable(timeOut)

Categories

Resources