I still have a need to record the process of solving the bug. Transfer from http://fecbob.pixnet.net/blog/post/39322597-android-%3E-wifi-%3E-%E6%8E%83%E6%8F%8F%E9%99%84%E8%BF% 91wifi-and Http://stackoverflow.com/questions/8717741/application-never-receives-rssi-changed-action. This is my first Android program, for some of the details of the principle, but also need to study.
Package Com.example.wifi_scan;
Import java.util.List;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.net.wifi.ScanResult;
Import Android.net.wifi.WifiManager;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;
public class Mainactivity extends Activity {
/** called when the activity is first created. */
Private TextView Wifitext;
Private Wifimanager Wifimanager;
Private StringBuilder SB;
Private list<scanresult> wifilist;
Private Wifireceiver Wifireceiver;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
SB = new StringBuilder ();
Wifitext = (TextView) Findviewbyid (R.id.wifi);
Wifimanager = (Wifimanager) getsystemservice (Context.wifi_service);
Wifireceiver = new Wifireceiver ();
Registerreceiver (Wifireceiver, New Intentfilter (
wifimanager.scan_results_available_action));
Wifimanager.startscan ();
Wifitext.settext ("\nstarting scan\n");
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menu.add (0, 0, 0, "Refresh");
return Super.oncreateoptionsmenu (menu);
}
@Override
public boolean onmenuitemselected (int featureid, MenuItem item) {
Wifimanager.startscan ();
Wifitext.settext ("Starting Scan");
Return super.onmenuitemselected (Featureid, item);
}
@Override
protected void OnPause () {
Unregisterreceiver (Wifireceiver);
Super.onpause ();
}
@Override
protected void Onresume () {
Registerreceiver (Wifireceiver, New Intentfilter (
wifimanager.scan_results_available_action));
Super.onresume ();
Wifimanager Wifiman = (wifimanager) getsystemservice (Context.wifi_service);
Wifiman.startscan ();
}
Private Final class Wifireceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
Wifimanager Wifiman = (wifimanager) getsystemservice (Context.wifi_service);
Wifiman.startscan ();
SB = new StringBuilder ();
Wifilist = Wifimanager.getscanresults ();
for (int i = 0; i < wifilist.size (); i++) {
Sb.append (New Integer (i + 1). ToString () + ".");
Sb.append ((Wifilist.get (i)). ToString ()). Append ("\ n");
}
Wifitext.settext (Sb.tostring ());
}
}
}
In fact, there are quite a lot of online code is able to collect WiFi signal strength, but not found to be able to achieve the refresh, so I am also very puzzled how to do. And then find a self can use button to refresh, found that it can be refreshed, carefully contrast the next, found that there is a function in action Startscan, but whether I use the Timer,handler mechanism or receiver mechanism can not, Then see this blog http://blog.csdn.net/zd_1471278687/article/details/12158367 the following discussion, change scan_results_available_action to Rssi _changed_action, but found that the status is still unchanged, then I continue to Google Wifimanager Rssi CHANGED ACTION, and then found this http://stackoverflow.com/ Questions/8717741/application-never-receives-rssi-changed-action, according to the method inside, the receive changed, found on it!
From this problem-solving route, the firm idea is: can find someone to help, but ultimately solve the problem is always their own!
Auto Refresh get WiFi signal strength, Android