在这段代码中我正在搜索wifi接入点并在文本视图中显示结果....它在三星手机上工作正常但由于某种原因,在其他手机的情况下列表总是空的....
你可以帮我理解为什么列表总是空的吗?我将非常感激。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);
}
}
答案 0 :(得分:0)
看起来很奇怪你必须在清单中声明一个精细或粗略的位置许可。前段时间我自己遇到过这个问题。