开发者

Adding a list of scan results from Wifi to an intent and retrieving from a broadcast receiver?

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开发者_StackOverflow(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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜