简体   繁体   English

Android studio 3 webView mailto: 链接

[英]Android studio 3 webView mailto: links

Im trying to put together a simple app with a webView in a fragment.我试图将一个简单的应用程序与片段中的 webView 放在一起。

The html I am loading has a mailto: link which when clicked crashes the app.我正在加载的 html 有一个mailto:链接,单击该链接会使应用程序崩溃。 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.我在这里查看了一些答案,他们都说你必须使用一个很好的 WebViewClient 并且有各种各样的例子,但我无法让它们中的任何一个工作。 Anyone able to explain just how I go about it?谁能解释一下我是怎么做的?

Here is my current tab.java这是我当前的 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.正如我在使用WebViewClient评论中所说的那样,如果我理解正确的话,您将如何解决这个问题。

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.问题在于,当您单击该mailto:链接时,WebView 正在尝试打开默认邮件客户端,但 HTML 不发送 Intent,解决方案是您需要创建此 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() :然后方法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.我只想说,如果您使用的是 WebView,那么您应该附加一个 WebViewClient 以努力使用最佳实践。 It allows you to handle everything about that webview rather than it just being a static element.它允许您处理有关该 webview 的所有内容,而不仅仅是一个静态元素。 The method shouldOverrideUrlLoading() and onErrorReceived() are the least you should have when using a webview in your application.在应用程序中使用 webview 时, shouldOverrideUrlLoading()onErrorReceived()是您应该拥有的最少方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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