我写了这段代码,它不断显示可用的wifi接入点及其信号强度.... for循环在代码中追加扫描结果stringBuffer正在一些Android手机正常工作,但在一些手机如nexus 5它跳过“for循环”代码部分....如果任何人可以帮助我,我会非常不胜感激。 在此先感谢
以下是代码:
public class MainActivity extends AppCompatActivity {
Switch aswitch;
int n=1;
TextView text;
WifiManager wifi;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aswitch=(Switch)findViewById(R.id.myswitch);
wifi=(WifiManager)getSystemService(WIFI_SERVICE);
text=(TextView)findViewById(R.id.mytv);
aswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
if (isChecked && !wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
message="enabled";
text.setText(message);
} else if (!isChecked && wifi.isWifiEnabled()){
wifi.setWifiEnabled(false);
message="disabled";
text.setText(message);
}
}
});
MyBroadcastReceiver broadcast=new MyBroadcastReceiver();
registerReceiver(broadcast, new IntentFilter(wifi.SCAN_RESULTS_AVAILABLE_ACTION));
}
class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
StringBuffer stringBuffer=new StringBuffer();
List<ScanResult> list=wifi.getScanResults();
for(ScanResult scanResult:list){
String ssid0 = scanResult.SSID;
int rssi0 = scanResult.level;
String rssiString0 = String.valueOf(rssi0);
stringBuffer.append("\n SSID:" + ssid0 + " RSSI:" + rssiString0 +"dBm");
text.setText("yes");
}
text.setText(stringBuffer);
if(wifi.isWifiEnabled()){
wifi.startScan();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}