Wifi Connect-Disconnect Listener

Which listener should run my class to automatically check the code if Wi-Fi connects / disconnects?

I can manually check Wi-Fi connection / disconnection, but every time I need to connect / disconnect WIFI from Android settings, and then run my program for the result.

My current code is as simple as:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); if (wifi.isWifiEnabled()==true) { tv.setText("You are connected"); } else { tv.setText("You are NOT connected"); } 
+42
android wifi android wifi
Jun 15 '11 at 18:12
source share
5 answers

You are actually checking if Wi-Fi is turned on, which does not necessarily mean that it is connected. It just means that the Wi-Fi mode on your phone is turned on and can connect to Wi-Fi networks.

Here's how I listen to 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"); } }; 

To access active information about the network, you need to add the following permissions - to AndroidManifest.xml:

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

And the next receiver of intentions (or you can 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 can help if you want to perform an action when a user connects to an uncontrolled network connection. Take a look: http://developer.android.com/about/versions/android-5.0.html#Power




EDIT 2: Another consideration is that my answer does not verify that you have an internet connection. You can connect to a Wi-Fi network in which you need to log in. There is a useful check for "IsOnline ()" here: https://stackoverflow.com/a/167295/

+66
Oct. 15 '12 at 10:56
source share

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 the use of intent.

+17
Jun 15 '11 at 18:29
source share

Check out these two javadoc pages: ConnectivityManager WiFiManager

Please note that each defines broadcast actions. If you need to know more about registering broadcast receivers, check this out: Register a broadcast receiver programmatically

 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 can do something like this (if you prefer NOT to 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 must add that it would be better to register this broadcast receiver in your code and not in the manifest if you only need to receive the broadcast while the application is running. By specifying this in the manifest, your process will be notified of a connection change, even if it has not already been started.

+13
Jun 15 '11 at 18:25
source share

In AndroidManifest.xml, add the following permission.

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

Create a new recipient class.

 public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { Toast.makeText(context, "Wifi Connected!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "Wifi Not Connected!", Toast.LENGTH_SHORT).show(); } } 

And add this receiver class to the AndroidManifest.xml file.

 <receiver android:name="ConnectionChangeReceiver" android:label="NetworkConnection"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> 
+3
Feb 03 '15 at 9:02
source share

In response to the sabsab. To connect to the relay of communication translation, I used the response of warbi and added a class with static methods.

 public class WifiHelper { private static boolean isConnectedToWifi; private static WifiConnectionChange sListener; public interface WifiConnectionChange { void wifiConnected(boolean connected); } /** * Only used by Connectivity_Change broadcast receiver * @param connected */ public static void setWifiConnected(boolean connected) { isConnectedToWifi = connected; if (sListener!=null) { sListener.wifiConnected(connected); sListener = null; } } public static void setWifiListener(WifiConnectionChange listener) { sListener = listener; } } 

Then I made changes to the receiver class based on the first answer shown above.

 public class ConnectivityReceiver 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"); WifiHelper.setWifiConnected(true); } else { Log.d("WifiReceiver", "Don't have Wifi Connection"); WifiHelper.setWifiConnected(false); } } } 

Finally, in your activity, you can add a listener to use this callback.

 wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); wasWifiEnabled = (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED || wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING); WifiHelper.setWifiListener(new WifiHelper.WifiConnectionChange() { @Override public void wifiConnected(boolean connected) { //Do logic here } }); 

Note that the listener is deleted after the callback is triggered, because it is a static listener. In any case, this implementation works for me and is one way to capture your activity, or is it static somewhere.

+1
Nov 19 '16 at 1:17
source share



All Articles