Opening email client via Intent (but not to send a

2020-02-27 06:28发布

Is there a way to programically open email client, without a need to forcing message send? I just want the app to let user open his email client for email checking purposes :)

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    startActivity(Intent.createChooser(intent, ""));

This code works but it forces user to send a new message.

4条回答
戒情不戒烟
2楼-- · 2020-02-27 06:47

This code will show a dialog with a list of email clients. Clicking one will launch the application:

try {
    List<String> emailClientNames = new ArrayList<String>();
    final List<String> emailClientPackageNames = new ArrayList<String>();
    // finding list of email clients that support send email
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto", "abc@gmail.com", null));
    PackageManager pkgManager = AppController.getContext().getPackageManager();
    List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
    if (!packages.isEmpty()) {
        for (ResolveInfo resolveInfo : packages) {
            // finding the package name
            String packageName = resolveInfo.activityInfo.packageName;
            emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
            emailClientPackageNames.add(packageName);
        }
        // a selection dialog  for the email clients
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        builder.setTitle("Select email client");
        builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // on click we launch the right package
            Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
                startActivity(intent);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
} catch (ActivityNotFoundException e) {
    // Show error message
}
查看更多
不美不萌又怎样
3楼-- · 2020-02-27 06:53

I think you should replace Intent.ACTION_SEND to Intent.ACTION_VIEW,
i am sure this will work as this will prompt with list of application which support MIME type "message/rfc822" so it will include your default email client in your device other than gmail app.

How about this code:

final Intent emailLauncher = new Intent(Intent.ACTION_VIEW);
emailLauncher.setType("message/rfc822");
try{
       startActivity(emailLauncher);
}catch(ActivityNotFoundException e){

}
查看更多
Viruses.
4楼-- · 2020-02-27 06:57

In Kotlin, but this version creates a chooser for user to pick which email app to use. You basically do what Oved does in his answer, except create an actual chooser using LabeledIntent.

fun emailAppIntent(): Intent? {
    val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
    val packageManager = appLaunchContext.packageManager

    val activitiesHandlingEmails = packageManager.queryIntentActivities(emailIntent, 0)
    if (activitiesHandlingEmails.isNotEmpty()) {
        // use the first email package to create the chooserIntent
        val firstEmailPackageName = activitiesHandlingEmails.first().activityInfo.packageName
        val firstEmailInboxIntent = packageManager.getLaunchIntentForPackage(firstEmailPackageName)
        val emailAppChooserIntent = Intent.createChooser(firstEmailInboxIntent, "")

        // created UI for other email packages and add them to the chooser
        val emailInboxIntents = mutableListOf<LabeledIntent>()
        for (i in 1 until activitiesHandlingEmails.size) {
            val activityHandlingEmail = activitiesHandlingEmails[i]
            val packageName = activityHandlingEmail.activityInfo.packageName
            val intent = packageManager.getLaunchIntentForPackage(packageName)
            emailInboxIntents.add(
                LabeledIntent(
                    intent,
                    packageName,
                    activityHandlingEmail.loadLabel(packageManager),
                    activityHandlingEmail.icon
                )
            )
        }
        val extraEmailInboxIntents = emailInboxIntents.toTypedArray()
        return emailAppChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailInboxIntents)
    } else {
        return null
    }
}

.. then later call it with

val emailChooserIntent = emailAppIntent()

if (emailChooserIntent != null) {
    startActivity(emailAppIntent())
}

or handle null however you want.

查看更多
forever°为你锁心
5楼-- · 2020-02-27 07:01
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_EMAIL);
    startActivity(intent);  
    startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient)));

That kinda worked. But it opend Gmail for me, even since I have other email clients

查看更多
登录 后发表回答