https://kotlinlang.org logo
#android
Title
# android
b

Bin

01/17/2020, 11:14 AM
Copy code
fun onShareClick() {
        val intentShareList = ArrayList<Intent>()
        val shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.type = "text/plain"
        val resolveInfoList = packageManager.queryIntentActivities(shareIntent, 0)
        for (resInfo in resolveInfoList) {
            val packageName = resInfo.activityInfo.packageName
            val name = resInfo.activityInfo.name
            if (packageName.contains("com.facebook") ||
                packageName.contains("com.twitter.android") ||
                packageName.contains("<http://com.google.android.gm|com.google.android.gm>") ||
                packageName.contains("com.android.mms") ||
                packageName.contains("com.whatsapp")
            ) {
                val intent = Intent()
                intent.component = ComponentName(packageName, name)
                intent.action = Intent.ACTION_SEND
                intent.type = "text/plain"
                intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject")
                intent.putExtra(Intent.EXTRA_TEXT, "Your Content")
                intentShareList.add(intent)
            }
        }
        if (intentShareList.isEmpty()) {
            Toast.makeText(this@MainActivity, "No apps to share !", Toast.LENGTH_SHORT).show()
        } else {
            val chooserIntent = Intent.createChooser(intentShareList.removeAt(0), "Share via")
            chooserIntent.putExtra(
                Intent.EXTRA_INITIAL_INTENTS,
                intentShareList.toTypedArray()
            )
            startActivity(chooserIntent)
        }
    }
}
d

David Martin

01/18/2020, 5:54 AM
Have you tried to print out all values of resolveInfoList? is it contains 5 apps?
b

Bin

01/18/2020, 10:52 AM
yes, list contain all five but result showing like below attached image
3 Views