【问题标题】:Signal Strength not refreshing every few seconds信号强度不是每隔几秒刷新一次
【发布时间】:2014-03-19 05:36:20
【问题描述】:

我有一个代码可以告诉我 wifi 的信号强度。现在,我希望它每 5 秒刷新一次并告诉我新的信号强度。

  1. 它给出一组信号强度 5 次,每次它给我相同的信号强度。我不知道为什么。

  2. 一旦安装在我的 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);
                            }
                    };
    }

请帮助我。提前致谢。

【问题讨论】:

    标签: android runnable signal-strength


    【解决方案1】:

    你做错了。

    mainWifiObj.getScanResults(); 获取最后更新的扫描结果。这些不会立即更新。您需要拨打电话(您会这样做)以注册接收器以监听 wifi 信号的变化。接收器的onReceive 方法会在每次信号强度变化时自动被调用——所以你不需要一遍又一遍地调用你的Runnable。

    只需在WifiScanReceiveronReceive 方法中处理来自List&lt;ScanResult&gt; wifiScanList = mainWifiObj.getScanResults(); 的所有代码,然后在onResume 中注册此接收器,并在onPause 中注销它。

    您还将受益于查看 Android 上 WiFi 设置应用程序的源代码,该应用程序位于 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2017-04-03
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多