处理网络视图中的链接

发布于 2024-10-13 19:15:51 字数 2025 浏览 2 评论 0原文

我的 WebView 加载 web 视图中的所有链接 - 但是当我选择电子邮件链接时,它会尝试将其加载到 web 视图中,而不是在手机上启动电子邮件应用程序。我该如何解决这个问题?链接是 mailto://[email protected]

这是我的代码 - WebView 本身工作正常,所有内容都加载到 webview 中,包括 mailto: 等。需要 mailto 链接才能在其他地方加载

package com.apps.jerdog.crcc;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("unused")
public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    /**@Override */
    WebView webview;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("http://www.cedarridge.cc");
        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private class myWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            if (url.startsWith("mailto:") || url.startsWith("tel:")) { 
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(url)); 
                    startActivity(intent); 
                    } 
            view.loadUrl(url);
            return true;
        }
    }}

I have my WebView loading all links inside the webview - but when I select an email link it tries to load it in the webview instead of launching an email app on the phone. How do I resolve that? the links are mailto://[email protected]

Here is my code - the WebView itself works right and everything loads inside the webview, including mailto:, etc. Need the mailto links to load elsewhere

package com.apps.jerdog.crcc;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("unused")
public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    /**@Override */
    WebView webview;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("http://www.cedarridge.cc");
        }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private class myWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            if (url.startsWith("mailto:") || url.startsWith("tel:")) { 
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(url)); 
                    startActivity(intent); 
                    } 
            view.loadUrl(url);
            return true;
        }
    }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2024-10-20 19:15:51

我假设您已经覆盖了 shouldOverrideUrlLoading,您只需要处理这种特殊情况。

mWebClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("mailto:")){
                MailTo mt = MailTo.parse(url);
                Intent i = newEmailIntent(MyActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
                startActivity(i);
                view.reload();
                return true;
            }

                else{
                    view.loadUrl(url);
                }
                return true;
            }
       };
    mWebView.setWebViewClient(mWebClient);

    public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
      intent.putExtra(Intent.EXTRA_TEXT, body);
      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      intent.putExtra(Intent.EXTRA_CC, cc);
      intent.setType("message/rfc822");
      return intent;
}

I assume you are already overriding shouldOverrideUrlLoading, you just need to handle this special case.

mWebClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("mailto:")){
                MailTo mt = MailTo.parse(url);
                Intent i = newEmailIntent(MyActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
                startActivity(i);
                view.reload();
                return true;
            }

                else{
                    view.loadUrl(url);
                }
                return true;
            }
       };
    mWebView.setWebViewClient(mWebClient);

    public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
      intent.putExtra(Intent.EXTRA_TEXT, body);
      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      intent.putExtra(Intent.EXTRA_CC, cc);
      intent.setType("message/rfc822");
      return intent;
}
绳情 2024-10-20 19:15:51

从@schwiz 提供的答案来看,这是一个更干净的例子。假设 WebView 名为 webView

webView.setWebViewClient(new WebViewClient() {
    // shouldOverrideUrlLoading makes this `WebView` the default handler for URLs inside the app, so that links are not kicked out to other apps.
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // Use an external email program if the link begins with "mailto:".
        if (url.startsWith("mailto:")) {
            // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);

             // Parse the url and set it as the data for the `Intent`.
            emailIntent.setData(Uri.parse(url));

            // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of this application.
            emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Make it so.
            startActivity(emailIntent);
            return true;
        } else {  
            // Returning false causes WebView to load the URL while preventing it from adding URL redirects to the WebView history.
            return false;
        }
    }
});

我已使用所有 mailto: 选项对此进行了测试:多个电子邮件地址、CC、BCC、主题和身体。

Going off of the answer provided by @schwiz, here is a cleaner example. Assuming the WebView is named webView:

webView.setWebViewClient(new WebViewClient() {
    // shouldOverrideUrlLoading makes this `WebView` the default handler for URLs inside the app, so that links are not kicked out to other apps.
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // Use an external email program if the link begins with "mailto:".
        if (url.startsWith("mailto:")) {
            // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);

             // Parse the url and set it as the data for the `Intent`.
            emailIntent.setData(Uri.parse(url));

            // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of this application.
            emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Make it so.
            startActivity(emailIntent);
            return true;
        } else {  
            // Returning false causes WebView to load the URL while preventing it from adding URL redirects to the WebView history.
            return false;
        }
    }
});

I have tested this with all the mailto: options: multiple email addresses, CC, BCC, subject, and body.

痴骨ら 2024-10-20 19:15:51
::::
::::   

    webViewTerms = (WebView) v
            .findViewById(R.id.webview_termsAndCond);
    webViewTerms.setWebViewClient(new Client());
    webViewTerms.getSettings().setJavaScriptEnabled(true);
    //webViewTerms.getSettings().setBuiltInZoomControls(true);
    webViewTerms.loadUrl(getUrl()); //.loadUrl("https://www.google.com/");
::::
::::
::::
    private class Client extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);


        }

    }
::::
::::   

    webViewTerms = (WebView) v
            .findViewById(R.id.webview_termsAndCond);
    webViewTerms.setWebViewClient(new Client());
    webViewTerms.getSettings().setJavaScriptEnabled(true);
    //webViewTerms.getSettings().setBuiltInZoomControls(true);
    webViewTerms.loadUrl(getUrl()); //.loadUrl("https://www.google.com/");
::::
::::
::::
    private class Client extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);


        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文