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

前端 未结 13 704
庸人自扰
庸人自扰 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 05:51

    For me only WifiManager.NETWORK_STATE_CHANGED_ACTION works.

    Register a broadcast receiver:

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    registerReceiver(broadcastReceiver, intentFilter);
    

    and receive:

    @Override
    public void onReceive(Context context, Intent intent) {
    
        final String action = intent.getAction();
    
        if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            boolean connected = info.isConnected();
    
            //call your method
        }      
    }
    

提交回复
热议问题