将 Wifi 的扫描结果列表添加到意图并从广播接收器检索?
我想添加一个列表作为参数传递到意图中,然后从广播侦听器接收它,但我遇到了一些麻烦。我不知道如何将此列表作为额外内容放入意图中,或从中检索列表。我可以进入广播接收器。
//In my Main File: Everthing is registered and working.
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults);
List<ScanResult> scanResults = Some values;
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");
// Then Need to put the List<ScanResults> into the intent.
// ie: intent.putExtra("MyResults", scanResults);
Context.sendBroadcast(intent);
// 我的广播接收器里面应该有列表。
public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
// Need something here to get the list
// ie: List<ScanResult> scanResults = extras.getBundle("MyResults");
}
};
希望我对这个问题很清楚。我只需要将列表放入包中并从包(或意图)中获取列表。
ScanResult 的格式为 ["","","","","","",""] 如果有帮助的话。所以我猜它可能类似于多维数组。
任何帮助表示赞赏!谢谢
I want to add a list as a parameter to pass into an intent and then receive it from a broadcast listener, but I'm having some trouble. I cannot figure out how to put this List into the Intent as an extra, or retrieving the list from it. I can get into the broadcast receiver.
//In my Main File: Everthing is registered and working.
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults);
List<ScanResult> scanResults = Some values;
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");
// Then Need to put the List<ScanResults> into the intent.
// ie: intent.putExtra("MyResults", scanResults);
Context.sendBroadcast(intent);
// My broadcast receiver that should have the list inside it.
public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
// Need something here to get the list
// ie: List<ScanResult> scanResults = extras.getBundle("MyResults");
}
};
Hopefully I am clear with this question. I just need to put the list into and get the List from the bundle (or intent).
A ScanResult is in the format of ["","","","","","",""] if that helps. So I guess it might be similar to a multidimensional array.
Any help is appreciated! Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经弄清楚了。我喜欢简单的解决方案,这几乎是最简单的。
Intent.putParcelableArrayListExtra("ScanResults", (ArrayList) scanResults);
并将其添加到广播接收器
ArrayList scanResults = extras.getParcelableArrayList("ScanResults");
所以最终的结果是:
希望这可以帮助处于类似情况的人。
I've figured it out. I like simple solutions, and this is pretty much as simple as it gets.
intent.putParcelableArrayListExtra("ScanResults", (ArrayList) scanResults);
And Add this to the Broadcast receiver
ArrayList scanResults = extras.getParcelableArrayList("ScanResults");
So the end result is:
Hopefully this helps out someone in a similar situation.