在 MainActivity 外部调用 wifi.startScan() 时不执行 wifi 扫描

wifi scan is not performed when calling wifi.startScan() outside the MainActivity

我正在开发一个加载地图(作为图像)的应用程序。我正在使用自定义视图来加载图像。每当用户触摸地图时,我都必须执行 wifi 扫描并存储结果。

我是这样声明wifi管理器的:

public static WifiManager wifi;

以便我可以在自定义视图中使用它。当我从自定义视图调用 wifi.startScan(); 时,它不执行扫描。它运行没有错误或异常,但没有可用的扫描结果。

这是我的广播接收器的代码:

   public class WifiReceiver extends BroadcastReceiver {


        /*
         * This receiver will set a flag in the main activity to indicate the new scan result has been set
         * it will set the new result too.
         * 
         */
        public void onReceive(Context c, Intent intent) {

            result=wifi.getScanResults();

            Log.d("surveyTool","scanning result set");


        }
        }

就了解扫描的工作原理而言,每当调用 wifi.startScan() 时,onReceiver() 中的代码应该在扫描结果可用时执行。但这并没有发生。

谁能告诉我这里出了什么问题??或者我应该怎么做才能从我的自定义视图开始扫描?

注意:我已经添加了所需的权限,如果我在 MainActivity 中调用它,扫描就会工作 **编辑:**

我已经使用这个在 MainActivity 中注册了接收器:

receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi,  new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

我在自定义视图中的 onTouch() 函数中调用 wifi.startScan()

    @Override
public boolean onTouch(View v, MotionEvent event) {

    // the coordinate of the touch point
    x=event.getX();
    y=event.getY();

    try {
        touchManager.update(event);

        // single point touch
        if (touchManager.getPressCount() == 1) {


            position.add(touchManager.moveDelta(0)); // to keep the point drawn at the touch location
            info.addToStartPoint(touchManager.moveDelta(0));

            if((touchManager.moveDelta(0).getX()==0 && touchManager.moveDelta(0).getY()== 0))
            {


                PreviousPoint= CurrentPoint;
                CurrentPoint=info.Map(x, y);


                if(CurrentPoint.getX()!=PreviousPoint.getX() && CurrentPoint.getY()!= PreviousPoint.getY())
                {
                    // the point is on the image
                    if(CurrentPoint.getX()!=-1 && CurrentPoint.getY()!= -1)
                    {
                        MainActivity.wifi.startScan();
                        MainActivity.counter++;
                        Log.d("surveyTool","touch: "+MainActivity.counter);
                        MainActivity.location=CurrentPoint;
                        MainActivity.newLocationAvailable=true; 
                        MainActivity.AddNewFingerprin();
                    }

                //  Log.d("surveyTool", " new touch event occurred");
                    // here do the surveying and other stuff    
                }

            }
        }
        else {
            if (touchManager.getPressCount() == 2) {

                Vector2D current = touchManager.getVector(0, 1);
                Vector2D previous = touchManager.getPreviousVector(0, 1);
                float currentDistance = current.getLength();
                float previousDistance = previous.getLength();

                if (previousDistance != 0) {
                    scale *= currentDistance / previousDistance; // for the zoom
                    info.setZoomFactor(scale);

                }// end of != 0
            }// end of if ==2
        }// end of else

        invalidate(); // will cause a redraw
    }
    catch(Throwable t) {

    }
    return true;
}

编辑

我在 onPause() 中注销接收器,然后在 onResume() 中重新注册。

可能是因为您没有在源代码中这样注册接收器

WifiReceiver wifiReciever = new WifiReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

如果您提供更多信息将有助于回答,所以如果这不起作用,请提供更多信息

由于您注销了 activity 的广播接收器 onPause() ,一旦您的 activity 被销毁,您将无法在任何时候调用 wifi.startScan()

您需要定义一个广播接收器并将其注册到 xml 中,以便能够收听 activity 之外的扫描结果。

为此制作接收器:

    public class ScanResultsReceiver extends BroadcastReceiver {

    @Override 
    public void onReceive(Context context, Intent intent) {

     //You need to initialise the wifimanager like this 
     private WifiManager wifiManager;
     wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     //Now get Scan Results
     List<ScanResult> results = wifiManager.getScanResults();     
    } 
}

在您的清单中添加

<receiver android:name=".wifiReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.net.wifi.SCAN_RESULTS"></action>
        </intent-filter>
    </receiver>
//Note: android:name  may differ according to your package structure

现在,无论何时您从任何 activity 或应用程序中的其他任何地方调用 wifiManager.startScan(),您都会收到系统广播