为什么应用程序停止扫描Wifi?

时间:2015-03-24 20:51:22

标签: java android android-wifi wifimanager

我已经制作了一个Android应用程序,其中一部分扫描wifi网络,并定期显示信号强度等信息与Toast。问题是,至少在棒棒糖上,它似乎在1-2次扫描后停止这样做,并且显示器上没有更多的祝酒词。这是我的代码:



public void startNavigation() {

        findViewById(R.id.startNavigation).setVisibility(View.GONE);

        Thread thread = new Thread() {

            public void run() {
                int i = 0;

                WiFiScanner.WiFiScanner(activity);

                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(globalcontext);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("signalCounter", 0);
                editor.commit();

                while(running) {
                    if(i%2 == 0) {
                        activity.runOnUiThread(new Runnable() {
                            public void run() {

                                findViewById(R.id.pointer).setVisibility(View.GONE);
                            }
                        });
                    } else {
                        activity.runOnUiThread(new Runnable() {
                            public void run() {
                                int signalCounter = WiFiScanner.getCounter();

                                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(globalcontext);
                                int oldCounter = sharedPreferences.getInt("signalCounter", 0);



                                if(signalCounter > oldCounter) {
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putInt("signalCounter", signalCounter);
                                    editor.commit();

                                    ArrayList<WiFiData> oldScan = WiFiScanner.getOldWifiList();
                                    ArrayList<WiFiData> newScan = WiFiScanner.getWifiList();
                                    ArrayList<WiFiData> intersection = new ArrayList<WiFiData>();

                                    for(int m=0;m<oldScan.size();m++) {

                                        if(oldScan.get(m).getRSS() < -64) {
                                            continue;
                                        }

                                        for(int n=0;n<newScan.size();n++) {
                                            if(oldScan.get(m).getBSSID().equals(newScan.get(n).getBSSID())) {
                                                intersection.add(oldScan.get(m));
                                                intersection.add(newScan.get(n));
                                            }
                                        }
                                    }

                                    int totalValues = 0;
                                    double summarizedDistance = 0;

                                    int p = 0;
                                    while(p < intersection.size()) {

                                        summarizedDistance += Position.getNewDistance(intersection.get(p + 1).getRSS(), intersection.get(p).getRSS(), intersection.get(p).getFrequency());


                                        totalValues++;
                                        p=p+2;
                                    }

                                    int meanMovedDistanceInPixel = 0;
                                    float angle = 0;

                                    if(totalValues > 0) {
                                        double meanMovedDistance = summarizedDistance/totalValues;

                                        angle = currentPosition - sharedPreferences.getFloat("startCompass", (float)0.0);
                                        if(angle < 0) {
                                            angle = 360+angle;
                                        }

                                        int scaleField = Integer.parseInt(sharedPreferences.getString("scaleFieldText", "0"));
                                        if(scaleField == 0) {
                                            return;
                                        }

                                        meanMovedDistanceInPixel = (sharedPreferences.getInt("seekBar", 0)*5)/scaleField * (int)meanMovedDistance;


                                        int posX = sharedPreferences.getInt("posX", 0);
                                        int posY = sharedPreferences.getInt("posY", 0);


                                        if(meanMovedDistanceInPixel != 0) {
                                            int[] newCoordinates = new int[2];
                                            newCoordinates = Position.getNewPosition(posX, posY, angle, meanMovedDistanceInPixel);

                                            int newX = newCoordinates[0];
                                            int newY = newCoordinates[1];

                                            editor.putInt("posX", newX);
                                            editor.putInt("posY", newY);
                                            editor.commit();

                                            setPosition(newX, newY);
                                        } else {
                                            setPosition(posX, posY);
                                        }

                                    }





                                    String values = "";

                                    values += "startangle " + String.valueOf(Math.abs(sharedPreferences.getFloat("startCompass", (float)0.0))) + "\n";
                                    values += "currentposition " + String.valueOf(currentPosition) + "\n";
                                    values += "angle " + String.valueOf(angle) + "\n";
                                    values += "scalefield " + sharedPreferences.getString("scaleFieldText", "0") + "\n";
                                    values += String.valueOf(meanMovedDistanceInPixel) + "\n";

                                    for(int j=0;j<oldScan.size();j++) {
                                        values += oldScan.get(j).getBSSID()+ ", " + oldScan.get(j).getRSS()+"\n";
                                    }

                                    values += "-----------\n";

                                    for(int j=0;j<newScan.size();j++) {
                                        values += newScan.get(j).getBSSID()+ ", " + newScan.get(j).getRSS()+"\n";
                                    }

                                    Toast.makeText(getApplicationContext(), String.valueOf(values), Toast.LENGTH_SHORT).show();

                                }

                                findViewById(R.id.pointer).setVisibility(View.VISIBLE);
                            }
                        });

                    }


                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                }
            }

        };

        thread.start();

    }
&#13;
&#13;
&#13;

这是wifi扫描程序的代码:

&#13;
&#13;
public class WiFiScanner {

    static BroadcastReceiver wifiScanReceiver;
    static Context globalcontext = null;
    private static  ArrayList<WiFiData> wifiList = new ArrayList<WiFiData>();
    private static ArrayList<WiFiData> oldWifiList = new ArrayList<WiFiData>();
    private static int counter = 0;

    public static void WiFiScanner(Context context) {

        globalcontext = context;

        final WifiManager wifimanager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        wifiList = new ArrayList<WiFiData>();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

        if (!wifimanager.isWifiEnabled()) {
            wifimanager.setWifiEnabled(true);

            try {
                Thread.sleep(2000);
            }catch (InterruptedException exception) {
                Thread.currentThread().interrupt();
            }
        }

        wifiScanReceiver = new BroadcastReceiver() {

            public void onReceive(Context context, Intent intent) {



                List<ScanResult> scanresult = wifimanager.getScanResults();

                oldWifiList = wifiList;
                wifiList = new ArrayList<WiFiData>();

                for (int i = 0; i < scanresult.size(); i++) {
                    WiFiData wifidata = new WiFiData(scanresult.get(i).level, scanresult.get(i).SSID, scanresult.get(i).frequency, scanresult.get(i).BSSID);
                    wifiList.add(wifidata);
                }

                counter++;
                wifimanager.startScan();

            }

        };

        globalcontext.registerReceiver(wifiScanReceiver, intentFilter);
        wifimanager.startScan();


    }

    public static int getCounter() {
        return counter;
    }

    public static ArrayList<WiFiData> getWifiList() {
        return wifiList;
    }

    public static ArrayList<WiFiData> getOldWifiList() {
        return oldWifiList;
    }

}
&#13;
&#13;
&#13;

你能帮我找一下造成这个问题的原因吗?奇怪的是,我记得它在另一台计算机上编译应用程序后在KitKat上正常工作。安卓工作室有什么问题吗?

1 个答案:

答案 0 :(得分:-1)

您是否将该应用作为服务运行?当手机屏幕关闭时,应用程序是否会暂停/停止?你尝试过使用Timer Task吗?或调度程序而不是thread.sleep?