最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I detect which application has been clicked in the Android native sharing dialog? - Stack Overflow

programmeradmin3浏览0评论

I would like to know how I can find out which application the user has clicked on in the content sharing dialog. I currently use this method:

    private fun showNativeShareDialog(contentUri: Uri) {
        val intent = Intent(Intent.ACTION_SEND).apply {
            type = "image/*"
            putExtra(Intent.EXTRA_STREAM, contentUri)
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }

        val chooserIntent = Intent.createChooser(intent, "Compartir imagen")
        shareResultLauncher.launch(chooserIntent)
    }

    private val shareResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == RESULT_OK) {
            val chosenComponent = result.data?.getStringExtra(Intent.EXTRA_CHOSEN_COMPONENT)
            chosenComponent?.let { packageName ->
                when {
                    packageName.contains("whatsapp") -> {
                        // Usuario eligió WhatsApp
                    }
                    packageName.contains("instagram") -> {
                        // Usuario eligió Instagram
                    }
                    // etc...
                }
            }
        }
    }

The problem I have with this, is that I depend on how the target application is configured, and normally when I share and go back, I get a resultCode of type RESULT_CANCELLED, is there a way to detect when it is pressed before navigating to the target app?

发布评论

评论列表(0)

  1. 暂无评论