Wifi Connect-Disconnect Listener Wifi Connect-Disconnect Listener android android

Wifi Connect-Disconnect Listener


Actually you're checking for whether Wi-Fi is enabled, that doesn't necessarily mean that it's connected. It just means that Wi-Fi mode on the phone is enabled and able to connect to Wi-Fi networks.

This is how I'm listening for actual Wi-Fi connections in my Broadcast Receiver:

public class WifiReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {             ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);         NetworkInfo netInfo = conMan.getActiveNetworkInfo();        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)             Log.d("WifiReceiver", "Have Wifi Connection");        else            Log.d("WifiReceiver", "Don't have Wifi Connection");        }   };

In order to access the active network info you need to add the following uses-permission to your AndroidManifest.xml:

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

And the following intent receiver (or you could add this programmatically...)

<!-- Receive Wi-Fi connection state changes --><receiver android:name=".WifiReceiver">    <intent-filter>        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />    </intent-filter></receiver>

EDIT: In Lollipop, Job Scheduling may help if you're looking to perform an action when the user connects to an unmetered network connection. Take a look: http://developer.android.com/about/versions/android-5.0.html#Power


EDIT 2: Another consideration is that my answer doesn't check that you have a connection to the internet. You could be connected to a Wi-Fi network which requires you to sign in. Here's a useful "IsOnline()" check: https://stackoverflow.com/a/27312494/1140744


Create your own class that extends BroadcastReceiver...

public class MyNetworkMonitor extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // Process the Intent here    }}

In AndroidManifest.xml

<receiver    android:name=".MyNetworkMonitor" >        <intent-filter>            <action android:name="android.net.wifi.STATE_CHANGE" />            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />        </intent-filter></receiver>

See WIFI_STATE_CHANGED_ACTION and CONNECTIVITY_ACTION for an explanation of using the Intent.


Check out these two pages of javadoc:ConnectivityManagerWiFiManager

Notice that each one defines broadcast actions. If you need to learn more about registering broadcast receivers, check this out:Programmatically register a broadcast receiver

BroadcastReceiver receiver = new BroadcastReceiver() {  @Override  public void onReceive(Context context, Intent intent) {    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);    if (wifi.isWifiEnabled()) {      tv.setText("You are connected");    } else {      tv.setText("You are NOT connected");    }  }};

And in your manifest you could do something like this (if you would prefer to NOT register the receiver in code):

<application android:icon="@drawable/icon" android:label="@string/app_name">    <receiver android:name=".WiFiReceiver" android:enabled="true">        <intent-filter>            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />        </intent-filter>    </receiver></application>

EDIT:

I should add that it would be better to register this broadcast receiver in your code rather than in the manifest if you only need to receive the broadcast while the app is running. By specifying it in the manifest your process will be notified of a connection change even when it was not previously running.

Apps targeting Android 7.0 (API level 24) and higher do not receive this broadcast if they declare the broadcast receiver in their manifest. Apps will still receive broadcasts if they register their android.content.BroadcastReceiver} with android.content.Context#registerReceiver Context.registerReceiver() and that context is still valid.