【发布时间】:2015-03-13 10:43:13
【问题描述】:
我正在尝试通过侦听“SUPPLICANT_CONNECTION_CHANGE_ACTION”来检测 WiFi 是否已连接,如下面的代码所示。但问题是 当我运行应用程序时,我没有收到来自我注册的广播接收器的通知!
为什么会发生这种情况以及如何解决?
代码:
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");
}
}
}
};
【问题讨论】:
-
清单中的所有权限集?
-
是的所有权限集,并且在运行时我没有在 logcat 中收到任何错误
-
您也已经在清单中注册了您的接收器吗?
-
@umeshlohani 不,我没有在清单中注册接收器,应该吗?该怎么做?
-
在下面的链接中关注dong221的回答stackoverflow.com/questions/9425187/…