Android Intent Chooser to only show E-mail option

2019-01-13 16:32发布

My app integrates e-mail where the user can submit a bug report, feedback, etc. from the app directly. I'm using the application/octet-stream as the SetType for the Intent. When you go to submit the e-mail you get the content chooser and it shows various items from Evernote, Facebook, E-mail, etc.

How can I get this chooser to only show E-mail so as not to confuse the user with all these other items that fit the content chooser type?

Thank you.

8条回答
迷人小祖宗
2楼-- · 2019-01-13 16:36

It works on all devices. It will show only Email Apps

public static void shareViaMail(Activity activity, String title, String body, String filePath) {

    Uri URI = Uri.parse("file://" + filePath);
    final Intent emailIntent = new Intent(Intent.ACTION_VIEW);

    emailIntent.setData(Uri.parse("mailto:"));

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact@brightsociety.com"});

    if (URI != null) {
        emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
    }
    try {
        activity.startActivity(emailIntent);
    } catch (Exception e) {
        ((BaseActivity) activity).showToast("Gmail App is not installed");
        e.printStackTrace();
    }
}
查看更多
冷血范
3楼-- · 2019-01-13 16:37

I solved this issue with simple lines of code as in the android documentation explain (https://developer.android.com/guide/components/intents-common.html#Email)

The most important is the flag: it is ACTION_SENDTO, and not ACTION_SEND

The other important line is

intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***

By the way, if you send an empty Extra, the if() at the end won't work and the app won't launch the email client.

This works for me. According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:

public void composeEmail(String[] addresses, String subject) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
查看更多
爷、活的狠高调
4楼-- · 2019-01-13 16:39

Just struggled with this problem while implementing a Magic Link feature, a chooser intent for all installed email apps:

Chooser Intent Screenshot

private void openEmailApp() {
  List<Intent> emailAppLauncherIntents = new ArrayList<>();

  //Intent that only email apps can handle:
  Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
  emailAppIntent.setData(Uri.parse("mailto:"));
  emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
  emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "");

  PackageManager packageManager = getPackageManager();

  //All installed apps that can handle email intent:
  List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);

  for (ResolveInfo resolveInfo : emailApps) {
    String packageName = resolveInfo.activityInfo.packageName;
    Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
    emailAppLauncherIntents.add(launchIntent);
  }

  //Create chooser
  Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
  chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
  startActivity(chooserIntent);
}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-13 16:39

It is possible to limit the choices of an intent chooser to just a few options. The code in the answer to this question is a good example. In essence, you would have to create a List of LabeledIntents to provide to the intent chooser, that will then include it in its list. Note that this solution works not on exclusion (certain apps are excluded while the rest remain) but instead you have to pick which apps to display. Hope it helps!

查看更多
冷血范
6楼-- · 2019-01-13 16:39

Solution is very simple:

Intent testIntent = new Intent(Intent.ACTION_VIEW);  
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme@me.com");  
testIntent.setData(data);  
startActivity(testIntent);  

See: http://www.gaanza.com/blog/email-client-intent-android/

查看更多
SAY GOODBYE
7楼-- · 2019-01-13 16:41

It works on all devices.It will show only Email Apps

public static void shareViaMail(Activity activity, String title, String body, String filePath) {
        Uri URI = Uri.parse("file://" + filePath);
        final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xyz@gmail.com"});
        /*if you want to attach something*/
        if (URI != null) {
            emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
        }
        try {
            activity.startActivity(emailIntent);
        } catch (Exception e) {
            ((BaseActivity) activity).showToast("Gmail App is not installed");
            e.printStackTrace();
        }
}
查看更多
登录 后发表回答