我有一个代码可以告诉我 wifi 的信号强度。现在,我希望它每 5 秒刷新一次并告诉我新的信号强度。
它给出了一组信号强度 5 次,每次它给我相同的信号强度。我不知道为什么。
一旦安装在我的 android 设备上,它就会给我信号强度,这与第一次安装时的信号强度答案相同。因此,每当我在 android 设备上运行应用程序时,我都会得到相同的答案。
代码是:
我在 dbm 中得到了答案。
public class MainActivity extends Activity {
protected static final long TIME_DELAY = 5000;
TextView mTextView;
Handler handler=new Handler();
int count =0; String data ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_id);
handler.post(updateTextRunnable);
}
@Override
public void onResume() {
super.onResume();
// Register the scan receiver
registerReceiver(wifiReciever);
}
@Override
public void onPause() {
super.onPause();
// Register the scan receiver
unregisterReceiver(wifiReciever);
}
Runnable updateTextRunnable = new Runnable() {
public void run() {
if (count < 5) {
StringBuilder sb = new StringBuilder();
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
for (ScanResult result : wifiScanList) {
if (result.SSID.equals("Khosla ka Ghosla")) {
sb.append(""+result.level);
}
if (result.SSID.equals("panny")) {
sb.append(""+result.level);
}
if (result.SSID.equals("ferbora")) {
sb.append(""+result.level);
}
}
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
count++; mTextView.setText("getting called " +count + sb);
} else {
}
//----------------code here to send values to java server---
handler.postDelayed(this, TIME_DELAY);
}
};
}
请帮我。提前致谢。