18

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.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name=".WifiReceiver" >
        <intent-filter>
            <action android:name="android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION" />
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

    //activity declaration here...

</application>

Do you guys know how to register the BraodcastReceiver in manifest?

I don't want to register it in activities because I want to monitor the wifi when there is changes in the wifi state whether my application is running or not.

This is my BroadcastReceiver class:

public class WifiReceiver extends BroadcastReceiver {
    private final String TAG = "WifiReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        String wifiStateText = "No State";

        switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLING:
            wifiStateText = "WIFI_STATE_DISABLING";
            break;
        case WifiManager.WIFI_STATE_DISABLED:
            wifiStateText = "WIFI_STATE_DISABLED";
            break;
        case WifiManager.WIFI_STATE_ENABLING:
            wifiStateText = "WIFI_STATE_ENABLING";
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            wifiStateText = "WIFI_STATE_ENABLED";
            break;
        case WifiManager.WIFI_STATE_UNKNOWN:
            wifiStateText = "WIFI_STATE_UNKNOWN";
            break;
        default:
            break;
        }
        MyLog.d(TAG, "onReceive Broadcast > WiFiState: " + wifiStateText);
        MyLog.d(TAG, "onReceive Broadcast > Time: " + new Date());
    }
}

I really hope to get some help. Thanks in advance.

Zul
  • 1,344
  • 2
  • 12
  • 21
  • Look at this Blog http://marakana.com/forums/android/examples/40.html – Ajay Feb 24 '12 at 04:34
  • Have you defined permission for android.permission.ACCESS_WIFI_STATE and android.permission.CHANGE_WIFI_STATE – Ajay Feb 24 '12 at 04:36
  • @Ajay, I already looked at that Blog you mentioned. The problem is, like I mentioned, I want to register the receiver in the manifest NOT in the activities. I already put the permission in the manifest. Anyway, thanks for your attention Ajay. – Zul Feb 24 '12 at 07:00
  • @Zul please help me for the following Broadcast receiver ... http://stackoverflow.com/questions/24989221/how-to-get-camera-click-event-with-the-help-of-broadcast-receiver – Amit Jayaswal Jul 28 '14 at 10:19

7 Answers7

31

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" />
dong221
  • 3,390
  • 6
  • 29
  • 31
  • Wow..thanks @dong221. That really did it. If u don't mind me asking, how did you know that the action name must be 'android.net.wifi.WIFI_STATE_CHANGED'? can you give me any link on that? – Zul Feb 24 '12 at 07:18
  • 1
    @Zul It's listed here as a constant value: http://developer.android.com/reference/android/net/wifi/WifiManager.html#WIFI_STATE_CHANGED_ACTION – Mark Whitaker Sep 15 '14 at 08:14
9

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

3
<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

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
user1203673
  • 1,015
  • 7
  • 15
  • Wow..thanks @user1203673. That really did it. If u don't mind me asking, how did you know that the action name must be 'android.net.wifi.WIFI_STATE_CHANGED'? can you give me any link on that? I'm sorry I cannot vote up, my reputation is not enough. – Zul Feb 24 '12 at 07:21
  • Try this: ` ` – Artificioo Nov 24 '16 at 19:08
0

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"/>
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
0

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.

Cytown
  • 1,539
  • 9
  • 7
0

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> 
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
Newts
  • 1,354
  • 14
  • 23
-1

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" />
M--
  • 25,431
  • 8
  • 61
  • 93
Ashwin Nirmale
  • 443
  • 4
  • 13