将 Wifi 的扫描结果列表添加到意图并从广播接收器检索?

发布于 2024-12-03 20:37:30 字数 1150 浏览 0 评论 0原文

我想添加一个列表作为参数传递到意图中,然后从广播侦听器接收它,但我遇到了一些麻烦。我不知道如何将此列表作为额外内容放入意图中,或从中检索列表。我可以进入广播接收器。

//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 技术交流群。

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

发布评论

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

评论(1

如痴如狂 2024-12-10 20:37:30

我已经弄清楚了。我喜欢简单的解决方案,这几乎是最简单的。

Intent.putParcelableArrayListExtra("ScanResults", (ArrayList) scanResults);

并将其添加到广播接收器

ArrayList scanResults = extras.getParcelableArrayList("ScanResults");

所以最终的结果是:

//In my Main File:
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults); 

List<ScanResult> scanResults = Some values; 
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");

intent.putParcelableArrayListExtra("ScanResults", (ArrayList<? extends Parcelable>) scanResults);

Context.sendBroadcast(intent);

// And my broadcast receiver 
   public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {

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

            Bundle extras = intent.getExtras();
    ArrayList<ScanResult> 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:

//In my Main File:
IntentFilter startUsingScanResults = new IntentFilter("StartUsingScanResults");
c.registerReceiver(serviceConsume.ScanResultReceiver, startUsingScanResults); 

List<ScanResult> scanResults = Some values; 
Intent intent = new Intent();
intent.setAction("StartUsingScanResults");

intent.putParcelableArrayListExtra("ScanResults", (ArrayList<? extends Parcelable>) scanResults);

Context.sendBroadcast(intent);

// And my broadcast receiver 
   public BroadcastReceiver ScanResultReceiver = new BroadcastReceiver() {

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

            Bundle extras = intent.getExtras();
    ArrayList<ScanResult> scanResults = extras.getParcelableArrayList("ScanResults");
}
};

Hopefully this helps out someone in a similar situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文