android - 广播接收器未收到意图.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 或者我需要获得的某种许可有关的一些问题从其他应用返回的意图
解决方案
推荐阅读
- reactjs - 如何在类内的函数中使用 React 中导入的图像?
- python - 如何替换Python中的第一个单词?
- javascript - Html 一次只为非单选按钮选择一个按钮
- c# - 使用 API 与 Skype Desktop 交互
- vuejs2 - Laravel Vuejs2 缺少命名路由的参数
- java - Spring:ApplicationContextAware 有价值,但@Autowired 没有
- python - factory_boy DjangoModelFactory 没有为测试创建模型条目
- sql - 如何在 PL/SQL 中编写语句级触发器
- python - 访问只有一个索引下标的列表元素的python列表
- python - Python - 将 UTC datetime.timetz() 转换为本地时间