BroadcastReceiver declared in manifest is not receiving the Broadcast

前端 未结 7 1881
余生分开走
余生分开走 2020-12-30 04:30

I\'ve tried to register a Wifi BroadcastReceiver to get the wifi state when it changes. But so far I have no luck receiving the broadcast.



        
7条回答
  •  隐瞒了意图╮
    2020-12-30 04:49

    The best that worked for me:

    Manifest:

    
        
            
        
    
    

    WifiReceiver 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) {
                if(info.isConnected()) {
                    //Do your work. 
                    //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:

    
    
    

提交回复
热议问题