I'm trying to implement broadcast receiver to determine which sharing option user clicked (via Intent.createChooser). The problem is broadcast receiver doesn't trigger onReceive. Here's the code :
object SharingHelper {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
fun shareProduct(
context: Context,
product: BaseProduct,
analyticsController: AnalyticsDataSource
) {
analyticsController.sharedProduct(product)
val packageManager = context.packageManager
val availablePackages = Intent(Intent.ACTION_SEND)
availablePackages.type = "text/plain"
val resInfo = packageManager.queryIntentActivities(availablePackages, 0)
val intentList = ArrayList<LabeledIntent>()
//here I create pending intent for the broadcast receiver
val pi = PendingIntent.getBroadcast(
context, 0,
Intent(INTENT_RECEIVER_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
// here i just branch the intent, depending on which app I want to share with
for (i in resInfo.indices) {
val resolveInfo = resInfo[i]
val packageName = resolveInfo.activityInfo.packageName
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
component = ComponentName(packageName, resolveInfo.activityInfo.name)
type = "text/plain"
}
if (packageName.contains(PACKAGE_FACEBOOK) || packageName.contains(PACKAGE_SMS)) {
sendIntent.putExtra(
Intent.EXTRA_TEXT,
context.getString(R.string.check_out_this_product_share) + " " + product.productUrl
)
}
else {
sendIntent.putExtra(Intent.EXTRA_TEXT, product.productUrl)
sendIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.check_out_this_product_share))
}
intentList.add(LabeledIntent(sendIntent, packageName, resolveInfo.loadLabel(packageManager), resolveInfo.icon))
}
//here is the creation of Chooser
val openInChooser = Intent.createChooser(Intent(),context.getString(R.string.share_via),pi.intentSender)
val extraIntents = intentList.toTypedArray()
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents)
context.startActivity(openInChooser)
}
private const val PACKAGE_SMS="android.mms"
private const val PACKAGE_FACEBOOK="facebook"
const val INTENT_RECEIVER_ACTION="ACTION_RETURN_CHOSEN"
}
In my fragment I do the following:
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d("xdd received", intent.toString())
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
requireActivity().registerReceiver(receiver,
IntentFilter(SharingHelper.INTENT_RECEIVER_ACTION))
And then I trigger share after clicking an icon:
override fun showShareDialog() {
SharingHelper.shareProduct(requireActivity(), viewModel.baseProduct, analyticsController)
}
I have checked the broadcast receiver with requireActivity().sendBroadcast in onViewCreated and the receiver does receive the intent so I my guess is it's not connected to broadcast receiver registration, but rather some issues either with context, pending intent/createChooser or maybe some sort of permission I need to get the returning intent from other apps