如何跟踪多个广播接收器
我有两个不同的文件需要初始化相同的广播接收器。我在检索结果时遇到问题。不知何故,我有时会得到空值,这是由于无法正确引用启动扫描的类而导致的。
这是一个代码示例。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
else if
中,您使用 main,但 main 为 null。也许它需要如下所示:in your
else if
you use main, but main is null.. Maybe it need to look like this: