簡體   English   中英

為什么應用程序停止掃描Wifi?

[英]why does the application stop scanning for Wifi?

我制作了一個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(); } 

這是wifi掃描儀的代碼:

 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; } } 

您能幫我找出造成問題的原因嗎? 奇怪的是,我記得在另一台計算機上編譯該應用程序后,它在KitKat上可以正常工作。 Android Studio可能有問題嗎?

您是否正在將應用程序作為服務運行? 手機屏幕關閉時,應用程序是否會暫停/停止? 您是否嘗試過使用計時器任務? 或調度程序而不是thread.sleep?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM