How to exclude a specific application from ACTION_SEND Intent?

后端 未结 6 528
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 04:55

I have used the following codes to exclude facebook app from my app chooser:

 List targetedShareIntents = new ArrayList();
    In         


        
6条回答
  •  天涯浪人
    2020-12-19 05:22

    Updated Kotlin answer based on Ragu Swaminathan's answer

    fun share() {
    
            val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.type = "text/plain"
            val resInfoList = activity?.packageManager?.queryIntentActivities(shareIntent, 0)
    
            val shareIntentList = arrayListOf()
    
            if (resInfoList?.isNotEmpty() == true) {
                for (resInfo in resInfoList) {
                    val packageName = resInfo.activityInfo.packageName
                    if (!packageName.toLowerCase().contains("discord")) {
                        val intent = Intent()
                        intent.component = ComponentName(packageName, resInfo.activityInfo.name)
                        intent.action = Intent.ACTION_SEND
                        intent.type = "text/plain"
                        intent.`package` = packageName
                        intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
                        shareIntentList.add(intent)
                    }
                }
            }
    
            if (shareIntentList.isEmpty()) {
                Toast.makeText(activity, "No apps to share!", Toast.LENGTH_LONG).show()
            } else {
                val chooserIntent = Intent.createChooser(Intent(), "Choose app to share")
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntentList.toTypedArray())
                activity?.startActivity(chooserIntent)
            }
        }
    

提交回复
热议问题