7

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 ?

kapa
  • 77,694
  • 21
  • 158
  • 175
captaindroid
  • 2,868
  • 6
  • 31
  • 45
  • 2
    See e.g. http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts for previous answers. – mlc Apr 17 '12 at 15:31

3 Answers3

7

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;
}
dreamcoder
  • 1,233
  • 1
  • 11
  • 25
  • if I have wifi connected without working internet connection, then the code returning true. you have any alternate for this? – Mohan Kanakala Feb 15 '14 at 21:19
  • @MoHaNKRaJ Hi...sorry for late reply.. Above code is for all kind of network connection..So if Wifi is connected then it will obviously return true. If you want to check condition for only mobile data connection then instead of using `getAllNetworkInfo()` method..use `getNetworkInfo(ConnectivityManager.TYPE_MOBILE)` so this will check only for mobile data connection – dreamcoder Mar 03 '14 at 16:37
  • @dreamcoder Isn't it enough to check if info!=null then network is connected? – Dr.jacky Jul 07 '14 at 12:32
  • @dreamcoder Actually, I faced a issue with WiFi only, the WiFi router does not have internet connection(Simply switched on the WiFi router with out any connection), though we can able to connect to WiFi from mobile right? Here I should get false value as we have no access to internet. But I have connected to WiFi, the above mentioned method returning true, So, while accessing online content from mobile, I'm getting exceptions. – Mohan Kanakala May 25 '15 at 13:27
3

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.

Sam Clewlow
  • 4,293
  • 26
  • 36
1
NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;