9

I am trying to share some text, In Android 10 always show max 3 apps in my mobile have WhatsApp but this not displaying here

This code is properly working in below 10 devices but could not find the reason why in android 10 is filtering out.

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

In the intentShareList contains WhatsApp info but not displaying

Edric
  • 24,639
  • 13
  • 81
  • 91
Bin
  • 91
  • 1
  • 3

3 Answers3

4

I had the same issue. It seems the problem is in some Android 10 devices (like Samsung), not all. I solved using Intent.EXTRA_EXCLUDE_COMPONENTS instead of Intent.EXTRA_INITIAL_INTENTS, and creating an array of ComponentName to be excluded. Refactoring your code:

fun onShareClick() {
    val excludedComponents = ArrayList<ComponentName>()
    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("com.google.android.gm") ||
            packageName.contains("com.android.mms") ||
            packageName.contains("com.whatsapp"))
        ) {
            excludedComponents.add(ComponentName(packageName, name))
        }
    }
    if (excludedComponents.size()==resolveInfoList.size()) {
        Toast.makeText(this@MainActivity, "No apps to share !", Toast.LENGTH_SHORT).show()
    } else {
        val chooserIntent = Intent.createChooser(shareIntent , "Share via")
        chooserIntent.putExtra(
            Intent.EXTRA_EXCLUDE_COMPONENTS,
            excludedComponents.toTypedArray()
        )
        startActivity(chooserIntent)
    }
}
2

I think you should create your chooser using the initial intent and add your intentShareList as extra, like this:

val chooserIntent = Intent.createChooser(shareIntent, "Share via")
        chooserIntent.putExtra(
            Intent.EXTRA_INITIAL_INTENTS,
            intentShareList.toTypedArray()
        )
        startActivity(chooserIntent)

I think it will bring what you want. Let me know if it helps :)

EDIT: Even if you put on your shareIntent a inicial EXTRA_INTENT and a type, Android Q will ignore what you added on EXTRA_INITIAL_INTENTS, so it wont help

I was reading about this on that issue and I think it wont be fixed, take a look = https://issuetracker.google.com/issues/134367295

Also, there some information on documentation that says using EXTRA_INITIAL_INTENTS to personalized data is not recomended, and its was made to helping share links and it can reduce the apps who will appear, take a look = https://developer.android.com/training/sharing/send

Well, Im very sad about that, Idk what do to also, maybe a unique text... :(

2

My answer may not answer OP's question but if you want to exclude a specified package, you can refer to it. This solution also works on pre-Android 11 OS.

For me, EXTRA_INITIAL_INTENTS doesn't work on Android 11. So I handled 2 flows for 2 cases (Android 11 or later and pre-Android).

The below code will exclude Facebook out of share options.

1. Add the Facebook package to Android Manifest for expanding package visibility on Android 11. You can check out

<manifest ...>
...

    <queries>
        ...
        <package android:name="com.facebook.katana" />
    </queries>
...
</manifest>

2. Create an intent chooser:

   private fun getShareChooserWithoutFacebook(
        context: Context,
        template: Intent
    ): Intent {

        // get available share intents
        val targets: MutableList<Intent> = mutableListOf()
        val candidates: List<ResolveInfo> =
            context.packageManager.queryIntentActivities(template, 0)
        val excludedComponents = ArrayList<ComponentName>()

        // remove facebook which has a broken share intent
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R) {
            for (candidate in candidates) {
                val packageName = candidate.activityInfo.packageName
                if (!packageName.equals("com.facebook.katana")) {
                    val target = Intent(Intent.ACTION_SEND)
                    target.type = "text/plain"
                    target.putExtra(
                        Intent.EXTRA_SUBJECT,
                        template.getStringExtra(Intent.EXTRA_SUBJECT)
                    )
                    target.putExtra(
                        Intent.EXTRA_TEXT,
                        template.getStringExtra(Intent.EXTRA_TEXT)
                    )
                    target.setPackage(packageName)
                    targets.add(target)
                }
            }
        } else {
            for (candidate in candidates) {
                val packageName = candidate.activityInfo.packageName
                val name = candidate.activityInfo.name
                if ((packageName.equals("com.facebook.katana"))) {
                    excludedComponents.add(ComponentName(packageName, name))
                }
            }
        }

        var chooserIntent = Intent.createChooser(template, context.getString(R.string.settings_send_via))

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R) {
            chooserIntent =
                Intent.createChooser(targets.removeAt(0), context.getString(R.string.settings_send_via))
            chooserIntent.putExtra(
                Intent.EXTRA_INITIAL_INTENTS,
                targets.toTypedArray()
            )
        } else {
            chooserIntent.putExtra(
                Intent.EXTRA_EXCLUDE_COMPONENTS,
                excludedComponents.toTypedArray()
            )
        }

        return chooserIntent

    }

3. Open the intent chooser

   fun openShareChooser(context: Context, title: String?, content: String?) {
        val template = Intent(Intent.ACTION_SEND)
        template.type = "text/plain"
        template.putExtra(Intent.EXTRA_SUBJECT, title)
        template.putExtra(Intent.EXTRA_TEXT, content)
        context.startActivity(
            getShareChooserWithoutFacebook(context, template)
        )
    }
Nayana Chandran
  • 1,416
  • 1
  • 16
  • 30
Tinh Huynh
  • 76
  • 2
  • 6