【问题标题】:Android studio 3 webView mailto: linksAndroid studio 3 webView mailto:链接
【发布时间】:2018-10-26 17:15:03
【问题描述】:

我试图将一个简单的应用程序与一个 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,然后您可以实现shouldOverrideUrlLoading() 方法,这将让您在需要时管理特定的网址

标签: android webview mailto


【解决方案1】:

正如我在评论中所说,使用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 的所有内容,而不仅仅是一个静态元素。 shouldOverrideUrlLoading()onErrorReceived() 方法是在应用程序中使用 webview 时最少应该使用的方法。

【讨论】:

  • 感谢您提供的信息,原来我的错误是第 8 层问题 :( 但以上内容帮助我实现了我所需要的。
猜你喜欢
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2012-06-18
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多