简体   繁体   English

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

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

I've made an android app a part of which scans for wifi networks and periodically displays information like signal strength with a Toast. 我制作了一个android应用程序,其中一部分扫描wifi网络并定期显示信息,例如Toast的信号强度。 The problem is that, on lollipop at least, it appears to stop doing that after 1-2 scans and there are no more toasts on the display. 问题是,至少在棒棒糖上,经过1-2次扫描后似乎停止这样做了,并且显示屏上没有烤面包了。 This is my code: 这是我的代码:

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

This is the code of the wifi scanner: 这是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; } } 

Can you help me find what is causing the problem? 您能帮我找出造成问题的原因吗? What is strange is that I remember it working correctly on KitKat after compiling the app on a different computer. 奇怪的是,我记得在另一台计算机上编译该应用程序后,它在KitKat上可以正常工作。 Is there any chance there is something wrong with Android studio? Android Studio可能有问题吗?

Are you running the app as a service? 您是否正在将应用程序作为服务运行? Does the app get paused/stopped when the phone screen turns off? 手机屏幕关闭时,应用程序是否会暂停/停止? Have you tried using a Timer Task? 您是否尝试过使用计时器任务? or scheduler instead of thread.sleep? 或调度程序而不是thread.sleep?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM