39

我有一个视图并查看该站点有发送电子邮件的 malito 代码。当我打开链接时出现错误。我希望当我打开链接时会打开 Gmail 应用程序或其他电子邮件应用程序。感谢所有的帮助者。

public class teacher extends Activity implements OnClickListener {
    WebView webView;
    final Activity activity = this;    
    @Override
    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.teacher);
        webView = (WebView) findViewById(R.id.webView145);    
        webView.getSettings().setJavaScriptEnabled(true);           webView.loadUrl("https://dl.dropboxusercontent.com/u/233211/%D7%A8%D7%A9%D7%99%D7%95%D7%9F/iWebKit%20demo/ther.html");    
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);    
                if (progress == 100)
                activity.setTitle(R.string.app_name); 
            }       });    
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
            // Handle the error
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;            }           });}
        public void onBackPressed() {    
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            Intent B = new Intent(this, MainActivity.class);
            startActivity(B);
        }}    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack() == true) {
                    webView.goBack();
                } else {
                    finish();
            }return true;}          }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }}
4

3 回答 3

57

您必须创建 WebViewClient 的子类并覆盖 mailto URL 加载。例子:

public class MyWebViewClient extends WebViewClient {
  private final WeakReference<Activity> mActivityRef;

  public MyWebViewClient(Activity activity) {
    mActivityRef = new WeakReference<Activity>(activity);
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {
      final Activity activity = mActivityRef.get();
      if (activity != null) {
        MailTo mt = MailTo.parse(url);
        Intent i = newEmailIntent(activity, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
        activity.startActivity(i);
        view.reload();
        return true;
      }
    } else {
      view.loadUrl(url);
    }
    return true;
  }

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

然后您必须将此自定义 WebViewClient 设置为您的 WabView:

webView.setWebViewClient(new MyWebViewClient(activity);
于 2013-09-18T14:43:26.160 回答
37

您应该使用以下内容更新您的 WebViewClient:

@SuppressWarnings("deprecation") 
@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    proceedUrl(view, Uri.parse(url))
    return true; 
} 

@TargetApi(Build.VERSION_CODES.N)
@Override 
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    proceedUrl(view, request.getUrl());
    return true; 
}

private void proceedUrl(View view, Uri uri){  
    if (uri.toString().startsWith("mailto:")) {
        startActivity(new Intent(Intent.ACTION_SENDTO, uri));
    } else if (uri.toString().startsWith("tel:")) {
        startActivity(new Intent(Intent.ACTION_DIAL, uri));
    } else { 
        view.loadUrl(uri.toString());
    } 
} 
于 2014-11-05T13:30:15.310 回答
26

shouldOverrideUrlLoading注意: -弃用Android Nougat 之后

您需要使用shouldOverrideUrlLoadingwith 以shouldOverrideUrlLoading获得更好的支持。

此外,您可能想检查 URL 是否有mailto:tel:,它们在 HTML5 中用于分别触发邮件客户端和电话拨号。

一个完整的解决方案现在看起来像这样

@SuppressWarnings("deprecation") 
@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {  
        //Handle mail Urls 
        startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
    } else if (url.startsWith("tel:")) {
        //Handle telephony Urls 
        startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
    } else { 
        view.loadUrl(url);
    } 
    return true; 
} 

@TargetApi(Build.VERSION_CODES.N)
@Override 
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    final Uri uri = request.getUrl();
    if (uri.toString().startsWith("mailto:")) {
        //Handle mail Urls 
        startActivity(new Intent(Intent.ACTION_SENDTO, uri));
    } else if (uri.toString().startsWith("tel:")) {
        //Handle telephony Urls 
        startActivity(new Intent(Intent.ACTION_DIAL, uri));
    } else { 
        //Handle Web Urls 
        view.loadUrl(uri.toString());
    } 
    return true; 
} 
于 2017-04-13T13:26:01.060 回答