android.net.wifi.STATE_CHANGE 启用 wifi 且 WifiManager.EXTRA_WIFI_STATE 不可用时不广播

android.net.wifi.STATE_CHANGE not broadcasting when wifi get enabled and WifiManager.EXTRA_WIFI_STATE not available

清单:

<receiver android:name=".triggers.WifiTrigger">
        <intent-filter>
            <action android:name="android.net.wifi.STATE_CHANGE" />
        </intent-filter>
    </receiver>

权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

接收器在 WiFi 禁用、连接或断开连接时执行(未检查 connecting/disabling/enabling),但在 WiFi 启用时不会触发,此外 WifiManager.EXTRA_WIFI_STATE extra 不可用我打算使用这个:

public class WifiTrigger extends BroadcastReceiver {
    ...
    @Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context);

    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
    int previousWifiState = intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
    log("p"+previousWifiState);
    log("c"+wifiState);
    //log is a defined method used for logging (it just calls Log.d(...,...)
    }
    ...
}

它会一直记录 p4 c4(WifiManager.WIFI_STATE_UNKNOWN 为 4)


您正在注册错误的 Intent 操作。您真正要找的是:

/**
 * Broadcast intent action indicating that Wi-Fi has been enabled, disabled,
 * enabling, disabling, or unknown. One extra provides this state as an int.
 * Another extra provides the previous state, if available.
 *
 * @see #EXTRA_WIFI_STATE
 * @see #EXTRA_PREVIOUS_WIFI_STATE
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WIFI_STATE_CHANGED_ACTION =
    "android.net.wifi.WIFI_STATE_CHANGED";

您注册的操作是针对网络状态更改,而不是适配器状态更改:

/**
 * Broadcast intent action indicating that the state of Wi-Fi connectivity
 * has changed. One extra provides the new state
 * in the form of a {@link android.net.NetworkInfo} object. If the new
 * state is CONNECTED, additional extras may provide the BSSID and WifiInfo of
 * the access point.
 * as a {@code String}.
 * @see #EXTRA_NETWORK_INFO
 * @see #EXTRA_BSSID
 * @see #EXTRA_WIFI_INFO
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String NETWORK_STATE_CHANGED_ACTION = 
    "android.net.wifi.STATE_CHANGE";

所以只需将您的接收器注册更改为:

<receiver android:name=".triggers.WifiTrigger">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

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

将解决您的问题。