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)
Related
I am developing the android app which runs my web service at app startup. My app is basically the home app of the device. So that's mean that When user will reboot the device, My app will come in front of him.
So far that is great. But I have noticed it that on app start the device takes 5 to 10 seconds to get the internet connected. whereas my web service gets run before the internet is established thus throwing an error of "An error occurred please try again later."
What I Want: Though I am showing that message I am really not satisfied as I really do want to show the user a Message. That message should be "Device is connecting to the internet"
but unfortunately, I am unable to do so. I am using the following code
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
but it really indicates me if the net is connected or not.
So is not there the case where I can show a message like "Please wait device is connecting to the internet"
Or how can I get that device is restarted not the only app. ... Please help
Register a local (or Manifest-declared if you want) broadcast receiver in your activity (documentation here https://developer.android.com/guide/components/broadcasts), listening to connectivity changes, using this intent filter: IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).
Then check if is the device has internet connection with the code you posted.
Remember to register your broadcast receiver inside onResume() method and to unregister it inside onPause() method
From api 24 and above:
you can use registerDefaultNetworkCallback https://developer.android.com/reference/android/net/ConnectivityManager.html#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)
The callback has a public method called "onAvailable" https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback.html#onAvailable(android.net.Network) that is called when a new network is ready to be used.
I am trying to connect to a WiFi network using WiFiManager. Here is the code I use to do so :
WifiManager manager = [...];
WifiConfiguration config = new WifiConfiguration();
manager.setWifiEnabled(true);
//Set SSID, PSK, etc...
[...]
//Connect to the network
int netId = manager.addNetwork(config);
if (netId == -1)
{
Toast.makeText(this, "Invalid PSK", Toast.LENGTH_LONG).show();
return;
}
manager.enableNetwork(netId, true);
manager.saveConfiguration();
Beforehand, I registered a BroadcastReceiver for the WifiManager.NETWORK_STATE_CHANGED_ACTION action. I'm trying to detect when the connection succeeds or fails (for various reasons including authentication) by checking the DetailedState of the given NetworkInfo.
Everything works when the connection succeeds, but for some reason my receiver isn't "executed" when the connection fails. The network is added to the device's list, I can see it by going into WiFi Settings, but it does not try to connect (or it does try to connect but doesn't notify my receiver).
Any clue ? Thanks !
I finally managed to achieve what I wanted.
To detect that the connection succeeded, register the WifiManager.NETWORK_STATE_CHANGED_ACTION broadcast intent and check for the NetworkInfo.DetailedState.OBTAINING_IPADDR detailed state (for WiFi obviously).
To detect that the connection failed, register the WifiManager.SUPPLICANT_STATE_CHANGED_ACTION broadcast intent and check for any WifiManager.EXTRA_SUPPLICANT_ERROR integer extra (typically WifiManager.ERROR_AUTHENTICATING). You may need to gain access for Android hidden API to achieve that.
The enableNetwork method return true if the connection status is success.
This works fine on success but is not the best method to check the failed status (return true if the connection failed after the first step authentication).
I think that you can check in the networkInfo object the actual connected network to see if the request is ok:
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
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;
}
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
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.