Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 959
Loui Avatar asked Dec 14 '13 01:12

Loui


2 Answers

This is my solution & it works for me. I hope it helps anyone else with the same issue I had.

@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);
                mail.setType("application/octet-stream");
                mail.putExtra(Intent.EXTRA_EMAIL, new String[]{"email address"});
                mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                mail.putExtra(Intent.EXTRA_TEXT, body);
                startActivity(mail);
                return true;
                }
            return true;
        }
like image 171
Loui Avatar answered Sep 19 '22 05:09

Loui


@Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {


            if(url.startsWith(TEL_PREFIX)) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            if (url.startsWith("mailto:")) {


                String body = "Enter your Question, Enquiry or Feedback below:\n\n";


                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                email.putExtra(Intent.EXTRA_TEXT, body);
                //need this to prompts email client only
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "Choose an Email client :"));


                return true;
                }
            return false;
        }
    });
like image 28
user1299132 Avatar answered Sep 21 '22 05:09

user1299132