Java – retrieve WiFi connection status (Android)
•
Android
I follow the following link to connect to my local WiFi. It works perfectly. I can connect to my local WiFi
However, I really want the current status of WiFi. I use the following code to get the status of WiFi. Unfortunately, I can only get 'Disable', 'enable' and 'scan' status. I really want to receive other intermediate status
Can you tell me what I did wrong?
public class ConnectionChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
Message msg = new Message();
msg.what = 5;
Bundle b = new Bundle();
String sStatus = "UnKNown";
switch(info.getDetailedState()){
case AUTHENTICATING:
sStatus = "Authenticating...";
break;
case CONNECTED:
sStatus = "Connected";
break;
case CONNECTING:
sStatus = "Connecting...";
break;
case DISCONNECTED:
sStatus = "Disconnected";
break;
case DISCONNECTING:
sStatus = "Disconnecting...";
break;
case Failed:
sStatus = "Failed";
break;
case IDLE:
sStatus = "Idle";
break;
case OBTAINING_IPADDR :
sStatus = "Obtaining IP Address...";
break;
case SCANNING:
sStatus = "Scanning...";
break;
case SUSPENDED:
sStatus = "Suspended";
break;
}
b.putString("status", sStatus);
msg.setData(b);
mHandler.sendMessage(msg);
}
}
I register a broadcast receiver as follows
IntentFilter filter = new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
resolvent:
Register a receiver:
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(mReceiver, mIntentFilter);
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
{
}else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
{
int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNowN);
LogUtil.d(LOG_SET, "+++++++-----------wifiStateReceiver------+++++++", DEBUG);
checkState(iTemp);
}else if(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
{
DetailedState state =WifiInfo.getDetailedStateOf((SupplicantState)
intent.getParcelableExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED));
changeState(state);
LogUtil.d(LOG_SET, "------------>>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<-------", DEBUG);
}
else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
{
DetailedState state=
((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
changeState(state);
}
}
private void changeState(DetailedState aState)
{
LogUtil.d(LOG_SET, ">>>>>>>>>>>>>>>>>>changeState<<<<<<<<<<<<<<<<"+aState, DEBUG);
if (aState == DetailedState.SCANNING)
{
Log.d("wifiSupplicanState", "SCANNING");
}
else if (aState == DetailedState.CONNECTING)
{
Log.d("wifiSupplicanState", "CONNECTING");
}else if(aState == DetailedState.OBTAINING_IPADDR)
{
Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
}
else if (aState == DetailedState.CONNECTED)
{
Log.d("wifiSupplicanState", "CONNECTED");
}
else if (aState == DetailedState.DISCONNECTING)
{
Log.d("wifiSupplicanState", "DISCONNECTING");
}
else if (aState == DetailedState.DISCONNECTED)
{
Log.d("wifiSupplicanState", "DISCONNECTTED");
}
else if (aState == DetailedState.Failed)
{
}
}
public void checkState(int aInt)
{
LogUtil.d(LOG_SET,"==>>>>>>>>checkState<<<<<<<<"+aInt, DEBUG);
if (aInt == WifiManager.WIFI_STATE_ENABLING)
{
Log.d("WifiManager", "WIFI_STATE_ENABLING");
}
else if (aInt== WifiManager.WIFI_STATE_ENABLED)
{
Log.d("WifiManager", "WIFI_STATE_ENABLED");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLING)
{
Log.d("WifiManager", "WIFI_STATE_DISABLING");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLED)
{
Log.d("WifiManager", "WIFI_STATE_DISABLED");
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码