Android每秒都会获得访问点详细信息

时间:2015-05-09 15:48:59

标签: android wifi

无论如何都要获取手机每秒都能检测到的接入点详细信息吗? 目前,我可以获取接入点详细信息,但不是每一秒。

我已经尝试过Thread和BroadcastReceiver,但接入点的详细信息只会在4-6秒后发生变化。编码可能是错误的。

主题:

handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while(Running){
                try{
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        number+=1;

                        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        List<ScanResult> results = wifiManager.getScanResults();
                        //int count = 1;
                        String WifiListDetails = "";

                        for (ScanResult result : results) {


                            WifiListDetails += number + ")" + result.SSID + "\n";
                        }

                        textViewControlTitle.setText(WifiListDetails);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();

广播接收器

    private IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
private int number = 0;

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        List<ScanResult> results = wifiManager.getScanResults();

        String WifiListDetails = "";

        for (ScanResult result : results) {

            WifiListDetails += number++ + ")" + result.SSID + "\n";
        }

        Toast.makeText(getBaseContext(),
                WifiListDetails,
                Toast.LENGTH_LONG).show();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


protected void onResume(){
    this.registerReceiver(receiver, filter);
    super.onResume();
}

protected void onPause(){
    this.unregisterReceiver(receiver);
    super.onPause();
}

其他人可能已经问过这个问题,但我找不到答案。

1 个答案:

答案 0 :(得分:0)

这个问题已经回答了。

  

其他人可能已经问过这个问题,但我找不到答案。

我进行了1次Google搜索,找到了this 您需要做的就是将其转换为尽可能快地搜索的线程(可能不是每隔一秒,而是每隔几秒)。

修改

要制作该主题,首先需要在主要活动中使用一些变量:

private Thread thread = null;
private boolean threadRunning = false;

然后你需要你的实际Thread Runnable对象,其代码为:

private final Runnable threadRunnable = new Runnable() {
    public void run() {
        while (threadRunning) {
            // your scanning code that needs to be executed every second goes here
            try { sleep(100); } catch(InterruptedException e) {}
            // Don't overclock, give the processor a break
            // Note: 100 means 0.1 seconds, so you shouldn't care about it
        }
    }
}

最后是您的startThreadstopThread

public void startThread() {
    threadRunning = true;
    thread = new Thread(threadRunnable);
    thread.start();
}
public void stopThread() {
    threadRunning = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        // Error at joining the thread
        // Thread JOIN means that no other thread may start before this one ends
        e.printStackStrace();
    }
}