views:

41

answers:

1

Hello, i am checking networking connection using the below code:

public static boolean haveInternet(Context ctx) 
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

   if (info == null || !info.isConnected()) {
          return false;  // no connection
   }

  return true;   // true if having connection
}       

Now, on "no connection" , i am launching "Wireless settings" dialog using the below code:

    context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

Here, user is being able to "on/off" the wireless, now how do i come to know whether the user has made "on" the wi-fi or not? i.e. what result is returned by the above intent on the successful wireless connection. how do i check it ?

I think i need to call startActivityForResult method, but dont know how do i do it ?

Update:

I want to do same way as http://groups.google.is/group/android-developers/msg/6874a5e4675dffdb

+1  A: 

now how do i come to know whether the user has made "on" the wi-fi or not? i.e. what result is returned by the above intent on the successful wireless connection. how do i check it ?

There is no result. You check it by calling the code you have shown above, or by monitoring relevant broadcast Intents (see ConnectivityManager and its CONNECTIVITY_ACTION or WifiManager and its WIFI_STATE_CHANGED_ACTION or NETWORK_STATE_CHANGED_ACTION).

I think i need to call startActivityForResult method, but dont know how do i do it ?

That activity does not support startActivityForResult().

CommonsWare
@CommonsWare thanx for the support, but how do i monitor the broadcast intents? i dont know about it, pls let me know
PM - Paresh Mayani
@Paresh Mayani: Register a `BroadcastReceiver` on an `IntentFilter` to watch for those actions. If only your activity cares about the changes, have it call `registerReceiver()` to register a `BroadcastReceiver` -- but be sure to call `unregisterReceiver()` at a later point. For example, here is an application that watches for the `ACTION_BATTERY_CHANGED` broadcast: http://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBattery/
CommonsWare
@CommonsWare i am really stucked because if there is having a connection, then i am fetching xml from the URL and if not (i.e. else), i am showing dialog, now dialog let you for the wireless settings, where i can set the wifi on/off , once i on/off the wi-fi , i returned back to the original activity by pressing "Back" button, now "connection" is available show it should fetch the xml, pls show me a way, pls
PM - Paresh Mayani
@CommonsWare i dont know anything about broadcast intent
PM - Paresh Mayani
@Paresh Mayani: http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/guide/topics/fundamentals.html#appcomp
CommonsWare
@Paresh Mayani: Plus, there is the example app I linked to previously: http://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBattery/
CommonsWare
@CommonsWare i found one link where broadcast related stuff are given: http://stackoverflow.com/questions/2294971/intent-action-for-network-events-in-android-sdk , are you talking about this, right ?
PM - Paresh Mayani
@Paresh Mayani: That is one way of doing it, though probably not the way you want. That puts the BroadcastReceiver in the manifest, which means you will get the broadcasts whenever they occur, regardless of whether your activity is running. In your case, I would think you would only care about the broadcasts while your activity is in use, in which case the sample application that I linked to (twice) is a more appropriate model.
CommonsWare