Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Calling networkInfo.isConnected()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nathan  
View profile  
 More options Jul 23 2012, 4:07 pm
From: Nathan <nathan.d.mel...@gmail.com>
Date: Mon, 23 Jul 2012 13:07:16 -0700 (PDT)
Local: Mon, Jul 23 2012 4:07 pm
Subject: Calling networkInfo.isConnected()

I wanted to ask a practical question about checking for network connection,
which is probably a simple question. I am sure this is an easy question.

I'm looking at this:

http://developer.android.com/training/basics/network-ops/connecting.html

"Before your app attempts to connect to the network, it should check to see
whether a network connection is available using getActiveNetworkInfo()<http://developer.android.com/reference/android/net/ConnectivityManage...>
 and isConnected()<http://developer.android.com/reference/android/net/NetworkInfo.html#i...>
."

This is in context of doing 10s of networks calls on multiple threads to
retrieve small files (about 20K) over http.

In practice, is there any cost for making this call? Does it initiate a
network operation or stay on the device? What percentage of the time is it
actually wrong? Would it be actually faster to see if one of my http
operations gets a sockettimeoutexception or a connectiontimeoutexception?

Based on that webpage, it calls isConnected() on a UI thread, just before
explaining that "network operations can involve unpredictable delays" and
should be done. Does that mean that isConnected() is a short and
predictable delay, or should I not read that much into it?

In one of the battery life classes at I/O, they talked about how the 3G
radio takes a bit to warm up, and that you might as well do a bunch of
network traffic while it is warm (paraphrasing a bit). Does simply calling
isConnected() warm up that radio?

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Jul 23 2012, 4:41 pm
From: Robert Greenwalt <rgreenw...@google.com>
Date: Mon, 23 Jul 2012 13:41:08 -0700
Local: Mon, Jul 23 2012 4:41 pm
Subject: Re: [android-developers] Calling networkInfo.isConnected()

This is checking static data in the ConnectivityService.  It doesn't leave
the device, but it will leave your process.  These calls don't do any
network activity and could be done on your UI thread.

Note that this check is not a promise that your request will complete - we
can lose contact at any time.  Given that, it's probably worth calling once
before you start a complex/expensive sequence, but not really worth calling
throughout.

Calling isConnected does not warm up the radio - only network traffic will
do that.

R


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan  
View profile  
 More options Jul 23 2012, 4:43 pm
From: Nathan <nathan.d.mel...@gmail.com>
Date: Mon, 23 Jul 2012 13:43:34 -0700 (PDT)
Local: Mon, Jul 23 2012 4:43 pm
Subject: Re: Calling networkInfo.isConnected()

Since this message, I did look at the code for NetworkInfo.isconnected()
and it is one line, doesn't look like it will block.

However, I am less certain about ConnectivityManager.getActiveNetworkInfo*(*
*)* since it does call a system service.

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan  
View profile  
 More options Jul 23 2012, 4:58 pm
From: Nathan <nathan.d.mel...@gmail.com>
Date: Mon, 23 Jul 2012 13:58:46 -0700 (PDT)
Local: Mon, Jul 23 2012 4:58 pm
Subject: Re: [android-developers] Calling networkInfo.isConnected()

Thank you, that is probably the info I needed.

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kostya Vasilyev  
View profile  
 More options Jul 23 2012, 5:44 pm
From: Kostya Vasilyev <kmans...@gmail.com>
Date: Tue, 24 Jul 2012 01:44:51 +0400
Local: Mon, Jul 23 2012 5:44 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

It's also a good way to check if the user disabled network connectivity
(i.e. the "mobile data" toggle), and starting with Android 4.0, this is
also affected by the "allow apps to perform background sync" system setting.

-- K

2012/7/24 Nathan <nathan.d.mel...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Jul 23 2012, 6:34 pm
From: Robert Greenwalt <rgreenw...@google.com>
Date: Mon, 23 Jul 2012 15:34:56 -0700
Local: Mon, Jul 23 2012 6:34 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

If you ask for the active network and verify it's connected you should not
need to check other settings (mobile data togle, background-sync, etc).
 They are all rolled in.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kostya Vasilyev  
View profile  
 More options Jul 23 2012, 7:24 pm
From: Kostya Vasilyev <kmans...@gmail.com>
Date: Tue, 24 Jul 2012 03:24:01 +0400
Local: Mon, Jul 23 2012 7:24 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

Yes, that was exactly my point. But the background sync is only rolled in
starting with Android 4.0, right?
 24.07.2012 2:36 пользователь "Robert Greenwalt" <rgreenw...@google.com>
написал:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Jul 23 2012, 8:00 pm
From: Robert Greenwalt <rgreenw...@google.com>
Date: Mon, 23 Jul 2012 17:00:49 -0700
Local: Mon, Jul 23 2012 8:00 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

Sorry Kostya - I misread your comment.

Yes - the background data preventer came out in ICS (4.0).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan  
View profile  
 More options Jul 23 2012, 8:24 pm
From: Nathan <nathan.d.mel...@gmail.com>
Date: Mon, 23 Jul 2012 17:24:50 -0700 (PDT)
Local: Mon, Jul 23 2012 8:24 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

On Monday, July 23, 2012 2:44:51 PM UTC-7, Kostya Vasilyev wrote:

> It's also a good way to check if the user disabled network connectivity
> (i.e. the "mobile data" toggle), and starting with Android 4.0, this is
> also affected by the "allow apps to perform background sync" system setting.

> -- K

REALLY?

It was all good news and I was set to use it until now. But now I could be
creating a support nightmare on 4.0.

That *would* make sense, if "background sync" were the only possible thing
that an app would use the internet for.
It *would* make sense if a user sees "no internet connection " in an app,
and immediately thinks, "oh, that must be because I disabled background
synching". Not likely. More likely, they'll forget they ever used that
setting and we will exchange ten emails.

Maybe I misunderstand what "allow apps to perform background sync" is for.
But I guarantee users will too.

Nathan

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Greenwalt  
View profile  
 More options Jul 23 2012, 8:38 pm
From: Robert Greenwalt <rgreenw...@google.com>
Date: Mon, 23 Jul 2012 17:38:34 -0700
Local: Mon, Jul 23 2012 8:38 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

In versions 4.0 and greater, if you app has background data turned off (by
the user) AND you are in the background and ask for the current active
network and check if it's connected, you will find it's not connected
(it'll be BLOCKED).  In fact, if you try to access the network you will
fail.

If either your background data setting is off OR you are in the foreground
you will be told you are connected (provided a connection, of course).

Before 4.0 there wasn't strict data enforcement - the data background stuff
was a suggestion that was not enforced.  Also the network status reported
was not app-specific.

I think that doing the calls you suggested should provide the correct
answer both pre-V4 and post-V4.  Not doing the calls just means you may
fail after spending alot of time and energy setting up network comms.

If you want to put up a different notice if they are explicitly blocked by
a user policy, you can watch for NetworkInfo.DetailedState.BLOCKED.

Let me know if you still think this is a problem.  We want to make APIs
that work for everybody.

R


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kostya Vasilyev  
View profile  
 More options Jul 24 2012, 5:28 am
From: Kostya Vasilyev <kmans...@gmail.com>
Date: Tue, 24 Jul 2012 13:28:20 +0400
Local: Tues, Jul 24 2012 5:28 am
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

2012/7/24 Robert Greenwalt <rgreenw...@google.com>

> If either your background data setting is off OR you are in the foreground
> you will be told you are connected (provided a connection, of course).

Ah, it's good to know there is a special case for when the app is in the
foreground. I don't remember this being documented, so thanks for
mentioning.

-- K


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan  
View profile   Translate to Translated (View Original)
 More options Jul 24 2012, 8:34 pm
From: Nathan <nathan.d.mel...@gmail.com>
Date: Tue, 24 Jul 2012 17:34:20 -0700 (PDT)
Local: Tues, Jul 24 2012 8:34 pm
Subject: Re: [android-developers] Re: Calling networkInfo.isConnected()

On Monday, July 23, 2012 5:38:34 PM UTC-7, Robert Greenwalt wrote:

> In versions 4.0 and greater, if you app has background data turned off (by
> the user) AND you are in the background and ask for the current active
> network and check if it's connected, you will find it's not connected
> (it'll be BLOCKED).  In fact, if you try to access the network you will
> fail.

> If either your background data setting is off OR you are in the foreground
> you will be told you are connected (provided a connection, of course).

I'm guessing a service, even with a foreground notification, is going to be
considered to be in the background, right?

It's possible that this has already gotten me a few support messages from
people saying that a download isn't making progress. But there are so many
user errors and server side errors that I'm not sure, even now, if this is
one of the first questions I will ask.

On Galaxy Nexus, the phone wouldn't let me access this setting unless I set
a data limit (assuming I found the right setting), gave me some warnings,
and posted what I think is a persistent notification. This might be enough
to make a user aware, but you never know since many users don't know how to
find notifications (the secret pulldown).  

In an activity, it does seem to make sense to note that data has been
blocked. For a batch download service, it seems like it would make sense to
check this at the very beginning and simply abort the operation, as this is
unlikely to change, unlike connectivity.

Nathan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »