简体   繁体   中英

Android studio 3 webView mailto: links

Im trying to put together a simple app with a webView in a fragment.

The html I am loading has a mailto: link which when clicked crashes the app. I've looked through a few answers on here and they all say you have to use a WebViewClient which is fine and there are various examples but I can't manage to get any of them to work. Anyone able to explain just how I go about it?

Here is my current tab.java

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Tab1Home extends Fragment {
    WebView webView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1home, container, false);
        WebView webView = (WebView) rootView.findViewById(R.id.webviewTab1);
        WebSettings settings = webView.getSettings();
        settings.setDefaultTextEncodingName("utf-8");
        settings.setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/tab1/tab1.html");
        return rootView;
    }

}

Thanks in advance.

As I said in my comment using WebViewClient is exactly how you would fix this, if I understand it correctly.

Your Problem:

The problem is that when you click that mailto: link, the WebView is trying to open the default mail client but the HTML doesn't send an Intent and the solution is you need to create this Intent.

Solution:

// add a WebViewClient to handle url requests
webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){

    //You can also use 'url.startsWith()'
    if (url.contains("mailto:")){
        MailTo mailTo = MailTo.parse(url);

        // make sure you have a context set somewhere in your activity, other wise use YOUR_ACTIVITY_NAME.this. 
        // For this example I am using mContext because that is my context variable
        Intent mailIntent = sendEmail(mContext, mailTo.getTo(), mailTo.getSubject(), mailTo.getBody()); // I added these extra parameters just incase you need to send those
        mContext.startActivity(mailIntent);
        view.reload(); // reload your webview using view.reload()

        return true;
    }else{
        // Handle what t do if the link doesn't contain or start with mailto:
        view.loadURL(url); // you want to use this otherwise the links in the webview won't work
    }
    return true;
   }
});

Then the method sendEmail() :

// Now you need the sendEmail method to open the devices mail client and set the relevant fields

private Intent sendEmail(Context context, String email, String subject, String body){

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.putExtra(Intent.EXTRA_EMAIL, email); // if you wanted to send multiple emails then you would use intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email })
intent.putExtra(Intent.EXTRA_TEXT, body); // optional if you have the body in your mailto: link
intent.putExtra(Intent.EXTRA_SUBJECT, subject); // optional if you have the subject in your mailto: link
intent.setType("text/plain");

return intent;
}

Ending Points:

I just want to say that if you're using a WebView then you shoudl attach a WebViewClient in an effort to use best practise. It allows you to handle everything about that webview rather than it just being a static element. The method shouldOverrideUrlLoading() and onErrorReceived() are the least you should have when using a webview in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM