如何跟踪多个广播接收器

发布于 2024-12-04 01:53:45 字数 1565 浏览 1 评论 0原文

我有两个不同的文件需要初始化相同的广播接收器。我在检索结果时遇到问题。不知何故,我有时会得到空值,这是由于无法正确引用启动扫描的类而导致的。

这是一个代码示例。

public void Connection_Logic (Context c)

    IntentFilter wififilter; 
    wififilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
    public WifiManager wifiManager;
    wifireceiver = serviceConsume.wifireceiver;
    c.registerReceiver(wifireceiver, wififilter);
    wifiManager.startScan();
}

public class serviceConsume {
    public WifiManager wifiMan;
    wifiMan = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    public BroadcastReceiver wifireceiver = new Wifi_Broadcast(this);
    IntentFilter wififilter; 
    wififilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
    registerReceiver(wifireceiver, wififilter);
    wifiMan.startScan();
}

public class Wifi_Broadcast extends BroadcastReceiver {

    public ServiceConsume main;
    public Connection_Logic ConnLog;

    public Wifi_Broadcast(ServiceConsume main) {
        super();
        this.main = main;
    }

    public Wifi_Broadcast(Connection_Logic ConnLog) {
        super();
        this.ConnLog = ConnLog;
    }

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

        if(main != null) {
            List<ScanResults> results = ConnLog.wifiManager.getScanResults();
        } else if (ConnLog != null) {
            List<ScanResults> results = ConnLog.wifiMan.getScanResults();
        }
    }
}

有谁有任何想法,为什么我可能会得到空值的结果?我这样做错了吗?如果有人有此工作的代码示例,我们将不胜感激。

谢谢!

I have 2 different files that need to initiale the same broadcast receiver. I am having problems with retrieving the results. Somehow I'm getting null values on occasion which is a result of not being able to properly reference the class that initiated the scan.

Here is a code example.

public void Connection_Logic (Context c)

    IntentFilter wififilter; 
    wififilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
    public WifiManager wifiManager;
    wifireceiver = serviceConsume.wifireceiver;
    c.registerReceiver(wifireceiver, wififilter);
    wifiManager.startScan();
}

public class serviceConsume {
    public WifiManager wifiMan;
    wifiMan = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    public BroadcastReceiver wifireceiver = new Wifi_Broadcast(this);
    IntentFilter wififilter; 
    wififilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
    registerReceiver(wifireceiver, wififilter);
    wifiMan.startScan();
}

public class Wifi_Broadcast extends BroadcastReceiver {

    public ServiceConsume main;
    public Connection_Logic ConnLog;

    public Wifi_Broadcast(ServiceConsume main) {
        super();
        this.main = main;
    }

    public Wifi_Broadcast(Connection_Logic ConnLog) {
        super();
        this.ConnLog = ConnLog;
    }

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

        if(main != null) {
            List<ScanResults> results = ConnLog.wifiManager.getScanResults();
        } else if (ConnLog != null) {
            List<ScanResults> results = ConnLog.wifiMan.getScanResults();
        }
    }
}

Does anyone have any ideas, why I might be getting null values for results? Am I doing this wrong? If anyone has a code example of this working, it'd be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-12-11 01:53:45
if(main != null) {
            List<ScanResults> results = ConnLog.wifiManager.getScanResults();
        } else if (ConnLog != null) {
            List<ScanResults> results = main.wifiMan.getScanResults();
        }

在您的 else if 中,您使用 main,但 main 为 null。也许它需要如下所示:

   if(main != null) {
                List<ScanResults> results = main.wifiMan.getScanResults();   
            } else if (ConnLog != null) {
                List<ScanResults> results = ConnLog.wifiManager.getScanResults();
            }
if(main != null) {
            List<ScanResults> results = ConnLog.wifiManager.getScanResults();
        } else if (ConnLog != null) {
            List<ScanResults> results = main.wifiMan.getScanResults();
        }

in your else if you use main, but main is null.. Maybe it need to look like this:

   if(main != null) {
                List<ScanResults> results = main.wifiMan.getScanResults();   
            } else if (ConnLog != null) {
                List<ScanResults> results = ConnLog.wifiManager.getScanResults();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文