android 手機gmail新增標籤 僅限電子郵件應用以解析意圖



手機gmail新增標籤 (8)

我有一個問題..我只想要電子郵件活動來解決意圖ACTION.SEND但在電子郵件旁邊我也得到其他應用程序(例如TubeMate),即使我已將mime類型設置為'message / rfc822'...任何想法如何我可以獲得電子郵件應用程序來解決它..


Answer #1

你也可以用:

//writes messages only to email clients
public void setWriteEmailButton() {
    btnWriteMail.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Intent.ACTION_SENDTO);
            i.setData(Uri.parse("mailto:"));
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{mConsultantInfos.getConsultantEMail()});
            i.putExtra(Intent.EXTRA_SUBJECT, mContext.getString(R.string.txtSubjectConsultantMail));
            i.putExtra(Intent.EXTRA_TEXT   , "");
            try {
                startActivity(Intent.createChooser(i, mContext.getString(R.string.txtWriteMailDialogTitle)));
            } catch (android.content.ActivityNotFoundException ex) {
                UI.showShortToastMessage(mContext, R.string.msgNoMailClientsInstalled);
            }
        }
    });
}

玩得開心(兩者結合;))


Answer #2
String recepientEmail = ""; // either set to destination email or leave empty
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);

關鍵是使用ACTION_SENDTO作為操作,使用mailto:作為數據。 如果您想讓用戶指定目標電子郵件,請使用mailto: 如果您自己指定電子郵件,請使用mailto:[email protected]

建議的方法過濾所有可以發送電子郵件的應用程序(例如默認的電子郵件應用程序或gmail)


Answer #3

以下是我發送帶附件的電子郵件(證明可以使用各種MIME類型的附件,甚至在同一封電子郵件中),並且只允許電子郵件應用程序(它還有一個解決方案,適用於沒有應用支持“mailto”的情況)。 首先,我們嘗試獲取支持mailto: format的活動。 如果沒有找到,那麼我們將獲得支持message/rfc822 MIME類型的所有活動。 我們選擇默認應用程序(如果有默認應用程序)或允許用戶從可用應用程序中進行選擇。 如果沒有應用支持mailto:message/rfc822 ,那麼我們使用默認選擇器。

public static void sendEmail(final Context p_context, final String p_subject, final String p_body, final ArrayList<String> p_attachments)
{
    try
    {
        PackageManager pm = p_context.getPackageManager();
        ResolveInfo selectedEmailActivity = null;

        Intent emailDummyIntent = new Intent(Intent.ACTION_SENDTO);
        emailDummyIntent.setData(Uri.parse("mailto:[email protected]"));

        List<ResolveInfo> emailActivities = pm.queryIntentActivities(emailDummyIntent, 0);

        if (null == emailActivities || emailActivities.size() == 0)
        {
            Intent emailDummyIntentRFC822 = new Intent(Intent.ACTION_SEND_MULTIPLE);
            emailDummyIntentRFC822.setType("message/rfc822");

            emailActivities = pm.queryIntentActivities(emailDummyIntentRFC822, 0);
        }

        if (null != emailActivities)
        {
            if (emailActivities.size() == 1)
            {
                selectedEmailActivity = emailActivities.get(0);
            }
            else
            {
                for (ResolveInfo currAvailableEmailActivity : emailActivities)
                {
                    if (true == currAvailableEmailActivity.isDefault)
                    {
                        selectedEmailActivity = currAvailableEmailActivity;
                    }
                }
            }

            if (null != selectedEmailActivity)
            {
                // Send email using the only/default email activity
                sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, selectedEmailActivity);
            }
            else
            {
                final List<ResolveInfo> emailActivitiesForDialog = emailActivities;

                String[] availableEmailAppsName = new String[emailActivitiesForDialog.size()];
                for (int i = 0; i < emailActivitiesForDialog.size(); i++)
                {
                    availableEmailAppsName[i] = emailActivitiesForDialog.get(i).activityInfo.applicationInfo.loadLabel(pm).toString();
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(p_context);
                builder.setTitle(R.string.select_mail_application_title);
                builder.setItems(availableEmailAppsName, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, emailActivitiesForDialog.get(which));
                    }
                });

                builder.create().show();
            }
        }
        else
        {
            sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, null);
        }
    }
    catch (Exception ex)
    {
        Log.e(TAG, "Can't send email", ex);
    }
}

protected static void sendEmailUsingSelectedEmailApp(Context p_context, String p_subject, String p_body, ArrayList<String> p_attachments, ResolveInfo p_selectedEmailApp)
{
    try
    {
        Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        String aEmailList[] = { "[email protected]"};

        emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, null != p_subject ? p_subject : "");
        emailIntent.putExtra(Intent.EXTRA_TEXT, null != p_body ? p_body : "");

        if (null != p_attachments && p_attachments.size() > 0)
        {
            ArrayList<Uri> attachmentsUris = new ArrayList<Uri>();

            // Convert from paths to Android friendly Parcelable Uri's
            for (String currAttachemntPath : p_attachments)
            {
                File fileIn = new File(currAttachemntPath);
                Uri currAttachemntUri = Uri.fromFile(fileIn);
                attachmentsUris.add(currAttachemntUri);
            }
            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentsUris);
        }

        if (null != p_selectedEmailApp)
        {
            Log.d(TAG, "Sending email using " + p_selectedEmailApp);
            emailIntent.setComponent(new ComponentName(p_selectedEmailApp.activityInfo.packageName, p_selectedEmailApp.activityInfo.name));

            p_context.startActivity(emailIntent);
        }
        else
        {
            Intent emailAppChooser = Intent.createChooser(emailIntent, "Select Email app");

            p_context.startActivity(emailAppChooser);
        }
    }
    catch (Exception ex)
    {
        Log.e(TAG, "Error sending email", ex);
    }
}

Answer #4

你也可以用這個。 它效率更高

Intent mailIntent = new Intent(Intent.ACTION_SEND);

  String mailTo[] = new String[] { "MAIL_TO_1", "MAIL_TO_2" };
  mailIntent.putExtra(Intent.EXTRA_EMAIL, mailTo);
  mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
  mailIntent.putExtra(Intent.EXTRA_TEXT, "MailsBody");
  mailIntent.setType("plain/text");

  startActivity(Intent.createChooser(mailIntent, "only email client"));

此代碼示例僅顯示當前安裝在您設備上的電子郵件客戶端


Answer #5

在Android中,沒有電子郵件活動這樣的東西。 還沒有可以創建的intent過濾器僅包含電子郵件應用程序。 每個應用程序(或活動)都可以定義自己的意圖過濾器。

因此,當使用意圖ACTION_SEND時,您將不得不依靠用戶智能從選擇器(而不是TubeMate)中挑選最喜歡的電子郵件應用程序。


Answer #6

這項工作對我來說只打開電子郵件應用程序:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:[email protected]"));
startActivity(intent);

Answer #7

當我使用意圖android.content.Intent.ACTION_SENDTO不適合我,因為它顯示許多應用程序,一些應用程序不是電子郵件客戶端。 我找到了這種方式,它對我來說非常有效。

Intent testIntent = new Intent(Intent.ACTION_VIEW);  
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "[email protected]");  
testIntent.setData(data);  
startActivity(testIntent);

Answer #8

嘗試這個

String subject = "Feedback";
            String bodyText = "Enter text email";
            String mailto = "mailto:[email protected]" +
                    "?cc=" + "" +
                    "&subject=" + Uri.encode(subject) +
                    "&body=" + Uri.encode(bodyText);

            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
            emailIntent.setData(Uri.parse(mailto));

            try {
                startActivity(emailIntent);
            } catch (ActivityNotFoundException e) {
                //TODO: Handle case where no email app is available
            }

圖片來源: https://medium.com/@cketti/android-sending-email-using-intents-3da63662c58fhttps://medium.com/@cketti/android-sending-email-using-intents-3da63662c58f





android-intent