我试图通过侦听代码中下面显示的“SUPPLICANT_CONNECTION_CHANGE_ACTION”来检测WiFi是否连接。但问题是当我运行应用程序时,我收到了来自我注册的广播接收器的通知!broadCast接收器永远不会被调用
为什么会发生这种情况,以及如何解决它?
代码:
IntentFilter intentFilter2 = new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConnectivityModule();
}
protected void ConnectivityModule() {
// TODO Auto-generated method stub
Log.d(TAG, "@interNetConnectivityModule: called");
registerReceiver(SupplicantReceiver, intentFilter2);
}
BroadcastReceiver SupplicantReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (supplicantState == (SupplicantState.COMPLETED)) {
Log.d(TAG, "@SupplicantReceiver: connected");
}
if (supplicantState == (SupplicantState.DISCONNECTED)) {
Log.d(TAG, "@SupplicantReceiver: not connected");
}
}
}
};
在清单中设置的所有权限? – Opiatefuchs 2015-03-13 10:45:49
是设置了所有权限,并且在运行时,我在logcat中没有收到任何错误 – rmaik 2015-03-13 10:52:01
您是否还在清单中注册了接收器? – 2015-03-13 11:18:57