我有一個代碼告訴我來自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);
}
};
}
請幫幫我。提前致謝。