I have problem with WiFi connection detection. My goal is to detect when user is switching between different WiFis. I found this, but it only detects when WiFi was established. In my case I need to know when one WiFi network changed to another on phone.
            Asked
            
        
        
            Active
            
        
            Viewed 78 times
        
    0
            
            
        - 
                    1once you get that notification, just check if the current wifi name is equal to previous one, and if not - the wifi network changed – Vladyslav Matviienko May 22 '18 at 08:25
- 
                    1Use `NETWORK_STATE_CHANGED_ACTION` for your BroadcastReceiver. It will give you a `CONNECTED` message. Then you can check as @VladyslavMatviienko suggested. – Ben May 22 '18 at 08:31
- 
                    thanks @Ben i will try it – Serg Burlaka May 22 '18 at 08:33
1 Answers
1
            You can use BroadcastReceiver
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(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                if(netInfo.isConnected()) {
                    WifiManager wifiManager = (WifiManager) context.getAplicationContext().getSystemService (Context.WIFI_SERVICE);
                    WifiInfo info = wifiManager.getConnectionInfo ();
                    String ssid  = info.getSSID();
                    Log.d("Wifi Connected", "Wifi name is "+ info.getSSID());
                }
        }
    }
}
 
    
    
        Serg Burlaka
        
- 2,351
- 24
- 35
 
    
    
        Eugen
        
- 877
- 6
- 16
- 
                    2That does not answer the question at all. They want to detect the switch from one network to another not check if they are connected to a Wifi. – Ben May 22 '18 at 08:30
- 
                    
- 
                    1@Eugen my studio recomends to use context?.applicationContext?.getSystemService(Context.WIFI_SERVICE) instead context?.getSystemService(Context.WIFI_SERVICE) – Serg Burlaka May 22 '18 at 09:14
- 
                    
