【问题标题】:Adding a list of scan results from Wifi to an intent and retrieving from a broadcast receiver?将来自 Wifi 的扫描结果列表添加到意图并从广播接收器中检索?
【发布时间】:2011-09-09 22:16:49
【问题描述】:

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

//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 的格式为 ["","","","","","",""]。所以我猜它可能类似于多维数组。

任何帮助表示赞赏!谢谢

【问题讨论】:

标签: java android list android-intent broadcastreceiver


【解决方案1】:

我想通了。我喜欢简单的解决方案,这非常简单。

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");
}
};

希望这对处于类似情况的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多