1

Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks

Androider
  • 21,125
  • 36
  • 99
  • 158

2 Answers2

0

Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes

Asahi
  • 13,378
  • 12
  • 67
  • 87
  • You have to picture the situation. Main activity is powering up. OnCreate() called its time to fetch the http data? But How do you WAIT WAIT until this happens. Perhaps you would make the Main activity a broadcast receiver? What broadcast are you then listening for? – Androider May 02 '11 at 09:58
  • Please be mroe specific. How do you use those to initiate net connect if you are not currently connected. And how do you wait for this? – Androider May 02 '11 at 10:49
  • To be more specific: - read documentation as suggested by MaxVT and myself; - look for related questions in SO and web in general. here is one: http://stackoverflow.com/questions/3262781/how-to-check-wifi-or-3g-network-is-available-on-android-device; In general - start your app with buttons disabled and then wait for event on network state change, upon receiving desired notification updated UI as needed. I am afraid I do not have a code snippet to demo it – Asahi May 02 '11 at 11:20
0

Use WifiManager to get the state of the connection.

Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.

MaxVT
  • 12,989
  • 6
  • 36
  • 50
  • what call would you make to try connecting? Assuming you wanted to try connecting to the default wifi network whatever the user might have selected? – Androider May 02 '11 at 09:56
  • Does this need to be put in an AsyncTask? – Androider May 02 '11 at 09:56
  • Is there a broadcast that would be issued when the WIFI actually connects if so what it it? – Androider May 02 '11 at 09:58
  • Please be specific. How do you wait and detect when network is connected. – Androider May 02 '11 at 10:49
  • 4
    Dear Androider, we are not paid to do all of the work for you. Asahi's and my answer are supposed to point you on the way to resolve your original question. Downvoting for not answering your torrent of follow-up questions that are no longer related to the original will only make me want to help you less. – MaxVT May 03 '11 at 08:24