Reputation: 12018
I am using following method to check if device has access to internet.
In this code i am trying to ping/request to www.google.com
try
{
HttpGet request = new HttpGet(new URI(WEB_SIT_TO_PING));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 500);
HttpConnectionParams.setSoTimeout(httpParameters, 500);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK)
{
result = true;
}
}
catch(Exception e)
{
e.printStackTrace();
result = false;
}
Above code works but, when my activity gets launched it displays one blank black screen for 1-2 seconds and then it displays the main screen of the activity.
I am doing this check in the onCreate
of the activity.
Is there another why of doing this check.
UPDATE:
I don't want to check if Network is connected or not, I want to check if Device has Access to Internet. It may be the case that Device has Network connection but that network connection does not have Internet Access.
Thanks.
Upvotes: 1
Views: 1568
Reputation: 81
The 1-2 seconds freeze you are facing is due to the http request you are making in onCreate method. The UI of your screen will not show up until your onCreate method has finished execution. If you still want to make an http request in onCreate method then you can make it in a separate thread. May be make an async task or a new thread whichever suits your need.
Always remember that never do time taking tasks in UI thread of android. Here you were making an http request in UI thread. As of now you are only experiencing a lag of 1-2 seconds, If you try the same thing when your android device is connected to WIFI network but internet is very slow then your http request may take upto 5 seconds to complete and if you try to do any UI operation during those 5 seconds your application will give "APPLICATION NOT RESPONDING" dialog.
Upvotes: 1
Reputation: 2173
I assume the following piece of code will help to check the network connection:
/*
*@return boolean return true if the application can access the internet
*/
public boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
Upvotes: 0
Reputation: 5980
In my own app I use the following piece of code:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
This checks the current network state of the phone to see if the device has a network connection, and returns a boolean variable based on this.
If you want to use this piece of code though, you'll have to add the following permission to your manifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Upvotes: 2
Reputation: 7011
You can check if the device is connected to a network easily enough or if you really want to see if the device can connect to Google specifically without the delay wrap that code in an ASync task.
Information on ASync task: http://developer.android.com/reference/android/os/AsyncTask.html
An AsyncTask will allow you to run the code on a thread seperate than the UI thread. Currently it's freezing for 1 - 2 seconds for that reason.
EDIT I haven't provided code for checking wireless connectivity as others have already provided other alternatives but I did think it was important to explain why you're experencing the freeze you are.
Upvotes: 2