2

Im developing an android app. For this app to work the Wi-Fi needs to be enabled all the time. Because the Wi-Fi is enabled it will keep on scanning for available networks.

I want some function to be called as soon as the Wi-Fi connects to a particular network. How do I achieve this?

I have written the following code but this works only once, how do I make this scan for networks continuously?

ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                      NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                      if(wifi.isConnected()){
                          final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                            if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
                              String ssid = connectionInfo.getSSID();
                            }
                          Log.i("wifi", "connected");
                      }
                      else{
                          Log.i("wifi", "not connected");
                      }
Archit Arora
  • 2,508
  • 7
  • 43
  • 69
  • see here http://stackoverflow.com/questions/5888502/how-to-detect-when-wifi-connection-has-been-established-in-android – Hardik May 15 '14 at 06:02

2 Answers2

2

Follow the steps and do a trick

1) Create NetworkChangeReceiver

public class NetworkChangeReceiver extends BroadcastReceiver {

    public static boolean isWifiConnected = true;
    public static final String tag = "NETWORKCHANGERECEIVER";

    @Override
    public void onReceive(final Context context, final Intent intent) {

        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi.isConnected()) {
            final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
                String ssid = connectionInfo.getSSID();
            }
            isWifiConnected = true;
            Log.i("wifi", "connected");
        } else {
            Log.i("wifi", "not connected");
            isWifiConnected = false;
        }
    }
}

2) Add this line to Manifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <receiver android:name="com.df.src.NetworkChangeReceiver" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

You should use a BroadcastReceiver in your application to get instant notifications about connectivity changes.

Some reference that may help you:

http://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/

http://developerandro.blogspot.com/2013/09/check-internet-connection-using.html?m=1

http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

rekaszeru
  • 19,130
  • 7
  • 59
  • 73