crazything
crazything

Reputation: 55

Intent.Createchooser() Broadcastreceiver is not called

I'm trying to figure out what app users pick when they share content from my app. To achieve this I'm using Inten.createChooser with a custom broadcast receiver. Unfortunately, I can't seem to get the receiver to actually be called.

Running Android 9, I've tried a few different combinations of receiver registration. Making it exported true/false, adding and removing intent-filters (though I can't really find any related to the chooser). The share-chooser itself works just fine and my images are shared. It's just the broadcastreceiver that is not triggering. I can see in logcat that PackageManager has found and registered the receiver.

AndroidManifest.xml (I am aware that exported -should- not be needed)

<receiver android:name=".receivers.ShareBroadcastReceiver"
            android:enabled="true"
            android:exported="false">
        </receiver

The code that creates the share intent (done in a fragment if that matters)

private fun startShareIntent(image: Bitmap){
        val receiver = Intent(context, BroadcastReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT)
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "image/jpg"
        // saveTempFile creates a temporary share:able file of the image and returns it's URI.
        intent.putExtra(Intent.EXTRA_STREAM, saveTempFile(image))

        if (intent.resolveActivity(context!!.packageManager) != null) {
            startActivity(Intent.createChooser(intent,
                    getString(R.string.share_menu_title),
                    pendingIntent.intentSender))
        }
    }
class ShareBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Log.d("ShareBroadcastReceiver", "Received broadcast")
}

Upvotes: 1

Views: 850

Answers (1)

Binary Baba
Binary Baba

Reputation: 2083

I believe the problem is in creating the Intent object. Instead of

val receiver = Intent(context, BroadcastReceiver::class.java)

it should be,

val receiver = Intent(context, ShareBroadcastReceiver::class.java)

Upvotes: 1

Related Questions