不调用Intent.Createchooser()Broadcastreciver

时间:2019-04-22 12:57:12

标签: android android-intent kotlin broadcastreceiver

我正在尝试找出用户共享我的应用程序内容时会选择哪些应用程序。为此,我将Inten.createChooser与自定义广播接收器结合使用。不幸的是,我似乎无法让接收方真正被呼叫。

运行Android 9,我尝试了几种不同的接收者注册组合。使它导出为true / false,添加和删除意图过滤器(尽管我真的找不到与选择器相关的任何内容)。共享选择器本身工作正常,并且共享了我的图像。只是广播接收器没有触发。我可以在logcat中看到PackageManager已找到并注册了接收者。

AndroidManifest.xml(我知道,-不需要-导出)

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

创建共享意图的代码(如果重要的话,在片段中完成)

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

1 个答案:

答案 0 :(得分:0)

我相信问题在于创建setState({[container]: container})对象。代替

Intent

应该是

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