繁体   English   中英

Android studio 3 webView mailto: 链接

[英]Android studio 3 webView mailto: links

我试图将一个简单的应用程序与片段中的 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;
    }

}

提前致谢。

正如我在使用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()是您应该拥有的最少方法。

暂无
暂无

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

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