首页 > 解决方案 > 广播接收器未收到意图.sender

问题描述

我正在尝试实现广播接收器来确定用户单击了哪个共享选项(通过Intent.createChooser)。问题是广播接收器没有触发onReceive。这是代码:

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

在我的片段中,我执行以下操作:

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))

然后我在单击图标后触发共享:

override fun showShareDialog() {
    SharingHelper.shareProduct(requireActivity(), viewModel.baseProduct, analyticsController)
}

我已经用 in 检查了广播接收器,requireActivity().sendBroadcast并且onViewCreated接收器确实收到了意图,所以我猜它没有连接到广播接收器注册,而是与上下文、待处理意图/createChooser 或者我需要获得的某种许可有关的一些问题从其他应用返回的意图

标签: androidkotlinbroadcastreceiver

解决方案


推荐阅读