我正在onResume()
上注册接收者:
registerReceiver(wifiConnectivityReceiver, new
IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));
这是接收者本身:
class WiFiConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED,false)){
Log.d(TAG,"Connected to network!");
} else {
Log.d(TAG,"Could not connect to network!");
}
}
}
在我的应用程序中,我可以连接到选定的WiFi网络,但是此
SUPPLICANT_CONNECTION_CHANGE_ACTION
永远不会触发。例如,如果将其更改为SUPPLICANT_STATE_CHANGED_ACTION
,它就可以正常工作。我正在研究ICS。
有其他 Intent 的人遇到过这样的问题吗?
最佳答案
我认为以下代码将对您有所帮助:
public void installMyReceiver(){
if (I) Log.i(TAG, "installMyReceiver() - Online");
mFlag = true;
myReceiver = new BroadcastReceiver(){
public void onReceive (Context context, Intent intent){
String action = intent.getAction();
if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)){
SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (supplicantState == (SupplicantState.COMPLETED)){
if (I) Log.i(TAG, "SUPPLICANTSTATE ---> Connected");
//do something
}
if (supplicantState == (SupplicantState.DISCONNECTED)){
if (I) Log.i(TAG, "SUPPLICANTSTATE ---> Disconnected");
//do something
}
}
}
};
IntentFilter mFilter = new IntentFilter (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver (myReceiver, mFilter);
}
这是获取所需信息然后执行一些操作的简单方法。
希望它能对您有所帮助!