问题描述
清单:
<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处于禁用,连接或断开状态(不检查连接/禁用/启用)时执行接收器,但启用WiFi时不会触发接收器,此外,WifiManager.EXTRA_WIFI_STATE
意图不可用,我正在使用这个:
The receiver is executed when WiFi is disabled, connected or disconnected (didn't check for connecting/disabling/enabling) but it is not triggered when WiFi is enabled, in addition WifiManager.EXTRA_WIFI_STATE
extra is not available in the intent, i am using this :
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为4.WIFI_STATE_UNKNOWN)
and it keeps logging p4
c4
(4 for WifiManager.WIFI_STATE_UNKNOWN)
推荐答案
您正在注册错误的Intent操作.您真正要寻找的是:
You're registering for the wrong Intent action. What you're really looking for is:
/**
* 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";
您要注册的操作是针对网络状态更改,而不是适配器状态更改:
The action you're registering for is for network state changes, not adapter state changes:
/**
* 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";
因此,只需从以下位置更改接收者注册即可:
So just changing your receiver registration from:
<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>
将解决您的问题.
这篇关于启用wifi且WifiManager.EXTRA_WIFI_STATE不可用时,android.net.wifi.STATE_CHANGE不广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!