Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

android - handling links in a webview

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;
        }
    }}
question from:https://stackoverflow.com/questions/4788461/handling-links-in-a-webview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...