Obtain the currently connected Wi-Fi information.
Wifimanager wifi_service = (wifimanager) getsystemservice (wifi_service );
Wifiinfo = wifi_service.getconnectioninfo ();
Wifiinfo has the following methods:
Wifiinfo. getbssid ();
Wifiinfo. getssid ();
Wifiinfo. getipaddress (); obtain the IP address.
Wifiinfo. getmacaddress (); obtain the MAC address.
Wifiinfo. getnetworkid (); obtain the network ID.
Wifiinfo. getlinkspeed (); get the connection speed to let the user know this information.
Wifiinfo. getarg ();Obtain the image strength of the received image. This allows you to directly connect to the Wi-Fi signal threshold provided by Huawei to provide users with the best connection effect by adjusting the network or geographical location.
The signal strength here depends onWifiinfo. getarg ();This method.
The obtained value is a range from 0 to-100. It is an int type data, where 0 to-50 indicates the best signal, and-50 to-70 indicates the signal deviation, if the value is less than-70, it indicates that the connection is the worst. It may fail to be connected or dropped.
Here I want to change the image based on the signal strength. Set the configuration file wifi_sel.xml as follows:
<Level-list xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: maxlevel ="50"Android: drawable =" @ drawable/library_template_05 "/>
<Item Android: maxlevel ="70"Android: drawable =" @ drawable/library_template_05_2 "/>
<Item Android: maxlevel ="100"Android: drawable =" @ drawable/library_template_05_3 "/>
</Level-List>
Note that all values are absolute values, because level must be absolute values in imageview. setimagelevel (level); otherwise, the program reports a null pointer.
The next step is to register the listener, which is similar to the android battery listener.
// WiFi related
Intentfilter wifiintentfilter; // WiFi listener
Add the following to the oncreate method:
// WiFi
Wifiintentfilter = new intentfilter ();
Wifiintentfilter. addaction (wifimanager. wifi_state_changed_action );
Then:
// Declare the WiFi Message Processing Process
Private broadcastreceiver wifiintentreceiver = new broadcastreceiver (){
@ Override
Public void onreceive (context, intent ){
Int wifi_state = intent. getintextra ("wifi_state", 0 );
Int level = math. Abs (wifimanager) getsystemservice (wifi_service). getconnectioninfo (). getarg ());
Log. I (Global. Tag, "1111:" + level );
Switch (wifi_state ){
Case wifimanager. wifi_state_disabling:
Log. I (Global. Tag, "1111:" + wifimanager. wifi_state_disabling );
Wifi_image.setimageresource (R. drawable. wifi_sel );
Wifi_image.setimagelevel (level );
Break;
Case wifimanager. wifi_state_disabled:
Log. I (Global. Tag, "2222:" + wifimanager. wifi_state_disabled );
Wifi_image.setimageresource (R. drawable. wifi_sel );
Wifi_image.setimagelevel (level );
Break;
Case wifimanager. wifi_state_enabling:
Wifi_image.setimageresource (R. drawable. wifi_sel );
Wifi_image.setimagelevel (level );
Log. I (Global. Tag, "33333:" + wifimanager. wifi_state_enabling );
Break;
Case wifimanager. wifi_state_enabled:
Log. I (Global. Tag, "4444:" + wifimanager. wifi_state_enabled );
Wifi_image.setimageresource (R. drawable. wifi_sel );
Wifi_image.setimagelevel (level );
Break;
Case wifimanager. wifi_state_unknown:
Log. I (Global. Tag, "5555:" + wifimanager. wifi_state_unknown );
Wifi_image.setimageresource (R. drawable. wifi_sel );
Wifi_image.setimagelevel (level );
Break;
}
}
};
Register in the onresume method and destroy it in the onpause method:
@ Override
Protected void onresume (){
Super. onresume ();
// Register a Wi-Fi message processor
Registerreceiver (wifiintentreceiver, wifiintentfilter );
}
@ Override
Protected void onpause (){
Super. onpause ();
Unregisterreceiver (wifiintentreceiver );
}
Last add permission:
<Uses-Permission Android: Name = "android. Permission. access_wifi_state"> </uses-Permission>
Note: QQ technology exchange group: add one if you are interested in 108614806.