Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if internet is available or not in app startup in android?

My app at first loads the datas from internet(I am using webservice) I want to check internet access at app startup.

  1. I will like to check if any forms of internet either 3G or WIFI or GPRS or any other is available or not.
  2. If not available, give message to user like "You need internet access" and exit the app. (Currently i am getting force close error in my app if there is no internet access)
  3. If availabe, start my app normally.
  4. Also, my app is fetches the datas from webservice at different phase, before each phase or operation, i will like to check internet access at first.

How do i do this ?

like image 520
captaindroid Avatar asked Apr 17 '12 15:04

captaindroid


People also ask

How do I know if an app is accessing the Internet?

Open up the Settings app and head to the Apps & notifications menu. Then, tap on the app you want to look at (if you can't spot it, tap See all). Tap on Permissions to see everything the app has access to: A messaging app, for instance, might have access to SMS. To turn off a permission, tap on it.

Why is my app not using internet?

Check if Data is Disabled for the App To change the setting again just follow these steps: Open the Settings app on your phone. Go to app management and find the app that is not able to use mobile data. Then go to data uses details of the app and turn on background data if it is disabled.


2 Answers

You can use my method:

public static 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++) 
            {
                Log.i("Class", info[i].getState().toString());
                if (info[i].getState() == NetworkInfo.State.CONNECTED) 
                {
                    return true;
                }
            }
        }
    }
    return false;
}
like image 122
dreamcoder Avatar answered Oct 02 '22 07:10

dreamcoder


You can do all this using ConnectivityManager. All the required info is available here

http://developer.android.com/reference/android/net/ConnectivityManager.html

You probably want to stick something like this in the onStart() method of your initial activity (depending on where in your code the connection is fired up and the data is downloaded)

ConnectivityManager cm =  (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm.getAllNetworkInfo().isConnected()) {
 //proceed with loading 
} else { 
//showErrorDialog 
}

I haven't tested te code so cutting and pasting is probably a bad idea, but this should give you a good starting point. There is plenty of other info if you check the docs.

Also it's might be an idea to handle the lack of connectivity by changing your code so it doesn't just crash if there is no connection, pre haps show a default loading screen? Also your app may fail to get data even if there is a connection available, so you'll want to handle that scenario too.

like image 33
Sam Clewlow Avatar answered Oct 02 '22 07:10

Sam Clewlow