BroadcastReceiver declared in manifest is not receiving the Broadcast

空扰寡人 提交于 2019-11-30 08:49:47

Your receiver in manifest should looks like this

<receiver android:name=".WifiReceiver" >
    <intent-filter>
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

Also the following permission may be needed

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
user1203673
<receiver android:name=".WifiReceiver" >
     <intent-filter android:priority="100" >
          <action
             android:name="android.net.wifi.WIFI_STATE_CHANGED"
             android:enabled="true" />
     </intent-filter>
</receiver>

Enter the high priority and also enabled flag as true

If your Target android version is more than Android O. if you declare receivers in manifest they wont work. So you need to register inside your activity.

Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.

Resource: https://developer.android.com/guide/components/broadcasts

Just because the broadcast was send with flag Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT, it means, your app must register this receiver before android boot up, which can only be the system services.

Newts

I think you have registered the receiver like this way:

<receiver
    android:name=".WiFiReciever">
    <intent-filter>
        <action
            android:name="class name with package" />
        <data
            android:scheme="myscheme" />
    </intent-filter>
</receiver> 
M. Usman Khan

The best that worked for me:

Manifest:

<receiver android:name="com.AEDesign.communication.WifiReceiver">
    <intent-filter android:priority="100">
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

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:

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

This is all broadcast receiver related to the internet connection

<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />                
<action android:name="android.net.wifi.STATE_CHANGE" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!