Android studio 3 webView mailto:链接

时间:2018-05-16 15:10:41

标签: android webview mailto

我试图在一个片段中组合一个带有webView的简单应用程序。

我正在加载的html有一个mailto:链接,点击该链接会导致应用崩溃。我在这里看了几个答案,他们都说你必须使用WebViewClient这很好,并且有各种各样的例子,但是我无法让它们中的任何一个起作用。有人能解释我是怎么做的吗?

这是我当前的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;
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

正如我在使用WebViewClient的评论中所说,如果我理解正确的话,你究竟是如何解决的。

您的问题:

问题是,当您单击mailto:链接时,WebView正在尝试打开默认邮件客户端,但HTML不会发送Intent,解决方案是您需要创建此Intent。

<强>解决方案:

// 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;
   }
});

然后方法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;
}

结束点

我只是想说,如果你使用的是WebView,那么你应该附加一个WebViewClient,以便使用最佳实践。它允许您处理有关该webview的所有内容,而不仅仅是一个静态元素。在您的应用程序中使用webview时,方法shouldOverrideUrlLoading()onErrorReceived()是最不应该的。