You certainly can have a race condition, but the more likely thing is either the network never had internet reach (walled garden) or temporarily doesn't have access to the internet. Both the cell radio and wifi have a tendency to hold on trying to get reconnected before they give up and report a disconnection.
Other than that, your code looks good. You can also cache this value and then listen for broadcasts rather than calling ConnectivityManager all the time - depends on how often you check this. On Tue, Jul 16, 2013 at 9:58 AM, Nathan <[email protected]> wrote: > The following is what I have seen as a fairly standard method for checking > for an internet connection. > > @Override > public void checkforConnection(Context ctx) { > // TODO Auto-generated method stub > ConnectivityManager mgr = (ConnectivityManager) > ctx.getSystemService(Context.CONNECTIVITY_SERVICE); > NetworkInfo ni = mgr.getActiveNetworkInfo(); > if(ni==null) > this.noConnection.set(true); > else if(!ni.isConnected()) > this.noConnection.set(true); > else > this.noConnection.set(false); > > } > > Is there any way this can fail? > > I have been using this for six months. Only two identifiable complaints. > They may not be sophisticated enough to know if they have an internet > connection, though. One has a friend that installs apps for him. > > Nathan > > -- > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Android Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.

