I see only 3 options when I want to see all the email clients on Android

Viewed 359

I have multiple email account installed on Android. But when I launch the Intent I see only three of them. I've added also the browsers installed. But still I can see only three options. Is there a way to show all the options available?

Select multiple options

This is the code I'm using:

       fun openEmailPicker(){
            val packageManager = activity.packageManager
            val resInfoEmail = extractResInfoFromIntent("mailto:", packageManager)
            val resInfoBrowser = extractResInfoFromIntent("http://www.gmail.com", packageManager)
            if (resInfoEmail.size > 0) {
                val openInChooser = buildHeader(resInfoEmail, packageManager)
                val emailIntents = createIntentLIst(1, resInfoEmail, packageManager)
                val browserIntents = createIntentLIst(0, resInfoBrowser, packageManager)

                browserIntents.addAll(emailIntents)

                openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, browserIntents.toTypedArray())
                startActivity(openInChooser)
            }
        }

        private fun buildHeader(resInfo: MutableList<ResolveInfo>, packageManager: PackageManager): Intent {
            val resolveInfo = resInfo[0]
            val intentChooser = packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName)
            return Intent.createChooser(intentChooser, getString(R.string.select_to_open_email))
        }

        private fun createIntentLIst(from: Int, resInfo: MutableList<ResolveInfo>, packageManager: PackageManager): MutableList<LabeledIntent> {
            val intentList: MutableList<LabeledIntent> = ArrayList()
            for (index in from until resInfo.size) {
                extractLabelToLabeledIntent(resInfo, index, packageManager, intentList)
            }
            return intentList
        }

        private fun extractLabelToLabeledIntent(resInfo: MutableList<ResolveInfo>, i: Int, pm: PackageManager, intentList: MutableList<LabeledIntent>) {
            val resolveInfo = resInfo[i]
            val packageName = resolveInfo.activityInfo.packageName
            val intent = pm.getLaunchIntentForPackage(packageName)
            intentList.add(LabeledIntent(intent, packageName, resolveInfo.loadLabel(pm), resolveInfo.icon))
        }

        private fun extractResInfoFromIntent(URI: String, packageManager: PackageManager): MutableList<ResolveInfo> {
            val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse(URI))
            return packageManager.queryIntentActivities(emailIntent, 0)
        }
2 Answers

You misunderstand what account and what application is. When you fire the intent, you get the list of the applications that can handle that intent. No matter how many separate accounts or whatever are created within these application. Once you got an application open, then you can select the account and the address -- you don't get a separate icon for every account within the same application in the intent chooser dialog.

You may get a list of the applications that are capable to handle your intent with queryIntentActivities() to receive a list of activities.

Official doc for App Chooser: https://developer.android.com/training/basics/intents/sending

I think those mail clients don't have action intent,

Any way you can add them manually by adding their package names something similar to this

List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareInent = new Intent(Intent.ACTION_SEND);
shareInent.setType("text/plain");
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(shareInent, 0);
// put the name of the packages you want in this ArrayList
ArrayList<String> wantedPackage = new ArrayList<>();

if (!resInfo.isEmpty()) {
    for (ResolveInfo info : resInfo) {
        Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
        targetedShare.setType("text/plain");
        String infoPackageName = info.activityInfo.packageName.toLowerCase();

        if (wantedPackage.contains(infoPackageName)) {
            targetedShare.putExtra(Intent.EXTRA_TEXT, "put your text here");
            targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
            targetedShareIntents.add(targetedShare);
            resPackageNames.add(infoPackageName);
        }
    }
    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Chooser title");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);
}

You can show the email clients in the chooser dialog but I don't think those clients would perform the action you desire since they lack the intent handling.

Related