在连接到WiFi接入点WIFI_STATE_CHANGED_ACTION意图没有收到? [英] WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?
问题描述
的我的应用程序的功能部分是扫描和显示的WiFi接入点的列表,并然后连接到所述一个由用户选择。我有这个功能的工作。现在,我也希望被通知当连接经过。这应该是相当简单的,但我发现自己绊脚石。
Part of my app's functionality is to scan and display a list of WiFi access points, and then connect to the one chosen by the user. I have this functionality working. Now, I also wish to be notified when the connection "goes through". This should be fairly simple, but I find myself stumbling.
我通过各种岗位阅读在这里SO,他们都提到在注册 WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION
或 WifiManager.WIFI_STATE_CHANGED_ACTION
。然而,无论这些作品对我来说。任何人都可以发现这个code任何错误? (我离开了该做的扫描和东西的部分)
I have read through various posts here at SO, and they all mention registering for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION
or WifiManager.WIFI_STATE_CHANGED_ACTION
. However, neither of these works for me. Can anyone spot any mistake in this code? (I am leaving out the parts which do the scan and stuff)
预期的行为:的一旦连接成功(例如,当我看到通知栏上的连接图标),广播应该被接收并我应该看到敬酒。
Expected behavior: As soon as the connection is successful (i.e, when I see the "connected" icon on the notification bar), the broadcast should be received and I should see the toast.
观察到的行为:的应用程序首次启动时,广播接收,每当我切换回它(即,每当 onResume()
被调用;或者我怀疑,每当我注册的意图)
Observed behavior: The broadcast is received when the app first starts, and whenever I switch back to it (i.e, whenever onResume()
is called; or I suspect, whenever I register for the intent)
public class WifiScanActivity extends Activity {
WifiManager mainWifi;
WifiReceiver mWifiReceiver;
IntentFilter mIntentFilter;
private final static String TAG = "WifiScanActivity";
public static final String INTENT_FOR_WIFI_CONNECTED =
WifiManager.WIFI_STATE_CHANGED_ACTION;
// public static final String INTENT_FOR_WIFI_CONNECTED =
// WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mWifiReceiver = new WifiReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mIntentFilter.addAction(INTENT_FOR_WIFI_CONNECTED);
registerReceiver(mWifiReceiver, mIntentFilter);
mainWifi.startScan();
}
protected void onPause() {
unregisterReceiver(mWifiReceiver);
super.onPause();
}
protected void onResume() {
registerReceiver(mWifiReceiver, mIntentFilter);
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
Log.d(TAG,
"In WifiReceiver: Broadcast Received = " + intent.getAction());
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent
.getAction())) {
// Display the ListView and connect to the selected AP
} else if (INTENT_FOR_WIFI_CONNECTED.equals(intent.getAction())) {
if (WifiManager.WIFI_STATE_ENABLED == intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE, 0)) {
displayNetworkInfo();
}
/*if(true == intent.getBooleanExtra(
* WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
* displayNetworkInfo();
}*/
}
}
}
private void displayNetworkInfo() {
WifiInfo wifiInfo = mainWifi.getConnectionInfo();
String ssid = wifiInfo.getSSID();
int ip = wifiInfo.getIpAddress();
String message = "Connection established.\nSSID = " + ssid
+ "; IP Address = " + Helper.displayIpAddress(ip);
Log.d(TAG, message);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
如果我去掉了$ C $下WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION,我没有看到在所有被接收的广播。
If I uncomment the code for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION, I don't see the broadcast being received at all.
注意的:我知道连接成功,因为我看到了Android的无线网络设置屏幕中的状态
Note: I know that the connection is successful because I see the status in Android's wifi settings screen.
推荐答案
好吧,我理解了它。事实证明,我被登记了错误的意图。我应该使用
WifiManager.NETWORK_STATE_CHANGED_ACTION
。
Ok, I figured it out. It turns out I was registering for the wrong intent. I should be using
WifiManager.NETWORK_STATE_CHANGED_ACTION
.
下面是的code相关部分的片段:
Here are the snippets of relevant portions of code:
mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
public void onReceive(Context c, Intent intent) {
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(NetworkInfo.State.CONNECTED.equals(nwInfo.getState())){//This implies the WiFi connection is through
displayNetworkInfo();
}
}
这篇关于在连接到WiFi接入点WIFI_STATE_CHANGED_ACTION意图没有收到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!