naesmanak
naesmanak

Reputation: 183

How to determine the chosen activity from an intentchooser android

I am trying to determine which intent the user selects from my custom intent chooser, but for whatever reason I can't get onReceive to fire. Here's a sample of my code:

val extraIntents = intentList.toTypedArray()
val receiver = Intent(context, broadcastReceiver.javaClass)
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0,                    
                                   receiver, PendingIntent.FLAG_UPDATE_CURRENT)
Intent.createChooser(intentList[0], "choose an intent..", 
                     pendingIntent.intentSender)
startActivityForResult(openInChooser, SELECTOR_CODE)

And earlier, I defined the broadcastReceiver:

broadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

I ran this code and set a break code on that onReceive method but it was never triggered. Any help would be greaatly appreciated!

Upvotes: 0

Views: 77

Answers (1)

TheWanderer
TheWanderer

Reputation: 17834

I'm fairly sure you can't set the Intent target to an anonymous BroadcastReceiver, or an inner BroadcastReceiver.

Make it a static sub-class or put it in its own file. You can still construct and register it dynamically. (Sidenote: remember to actually register it. It won't receive Intents if it's not registered.)

Alternatively, use a custom action and don't bother with the explicit target component. Dynamically registered BroadcastReceivers aren't subject to the implicit broadcast limitations in Oreo.

Upvotes: 1

Related Questions