How to detect when WIFI Connection has been established in Android?

前端 未结 13 747
庸人自扰
庸人自扰 2020-11-22 05:34

I need to detect when I have network connectivity over WIFI. What broadcast is sent to establish that a valid network connection has been made. I need to validate that a v

13条回答
  •  悲&欢浪女
    2020-11-22 06:02

    The best that worked for me:

    AndroidManifest

    
       
          
       
    
    

    BroadcastReceiver class

    public class WifiReceiver extends BroadcastReceiver {
    
       @Override
       public void onReceive(Context context, Intent intent) {
    
          NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
          if(info != null && info.isConnected()) {
            // Do your work. 
    
            // e.g. To check the Network Name or other info:
            WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String ssid = wifiInfo.getSSID();
          }
       }
    }
    

    Permissions

    
    
    

提交回复
热议问题