Webview "mailto:" link & "tel:" link work using Intent.ACTION_VIEW, but how do I add unique Subject ie for "mailto:" link

Viewed 12814

I've got this working as it should using the following code. All http urls open as they should within the webview, "tel:" link opens as it should in dialler, and "mailto:" link opens as it should in email client.

But my problem is how do I change the subject of the "mailto:" link to something different instead of its pre-defined subject. I'm guessing there should be 2 seperate intents, 1 for "tel:" link & 1 for "mailto:" link. I simply don't know how to put the code into the shouldOverrideUrlLoading method below. Or maybe I'm using the wrong method for what I require.

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }

            // Otherwise allow the OS to handle it
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(url));
            startActivity(intent);
            return true;
        }

I managed to get my own subject "mailto:" working with Intent, but without the "tel:" link included in code. So how can I do both plus use my own subject in "mailto:" link?

Any ideas or suggestions will be much appreciated!

4 Answers

Complete MainActivity for all mailto link recipient.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if( url.startsWith("http:") || url.startsWith("https:") ) {
        return false;
    }
    // Otherwise allow the OS to handle it
    else if (url.startsWith("tel:")) {
        Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
        startActivity(tel);
        return true;
    }
    else if (url.startsWith("mailto:")) {
        String body = "Enter your Question, Enquiry or Feedback below:\n\n";
        Intent mail = new Intent(Intent.ACTION_SEND);

        Intent intent = mail.setType("application/octet-stream");

        // use for any mailto link for the recipient
        MailTo recipient = MailTo.parse(url);
        mail.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient.getTo()});

        mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        mail.putExtra(Intent.EXTRA_TEXT, body);
        startActivity(mail);
        return true;
    }
    return true;
}
Related