简体   繁体   English

在Android WebViewClient中打开tel:和mailto:链接

[英]Opening tel: & mailto: links in Android WebViewClient

I have been looking for an answer to this question that I can understand for the last couple of days. 在过去的几天里,我一直在寻找一个我可以理解的问题的答案。 After trying all of the code snippets online that I have seen I am still having difficulties. 在网上尝试了所有代码片段后,我仍然遇到了困难。 I am very new to the android sdk and java, actually this is my first shot at writting an Android app. 我对android sdk和java非常陌生,实际上这是我编写Android应用程序的第一枪。 So my question is this, how come I keep getting the ever so famous error "Page cant be displayed" when clicking on a mailto or tel link ? 所以我的问题是,当单击mailto或tel链接时,为什么会一直收到如此著名的错误“页面无法显示”?

Here is my code: 这是我的代码:

package com.mine.mobile;

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

public class MineActivity 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.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://my.mobilesite.com");
    }

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("tel:")) {
        startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
        return true;
    } else if (url.startsWith("mailto:")) {
        url = url.replaceFirst("mailto:", "");
        url = url.trim();
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url});
        startActivity(i);
        return true;
    } else if (url.startsWith("geo:")) {
        Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
        startActivity(searchAddress); 
}
        else {
        view.loadUrl(url);
        return true;
    }
    return true;
}
}

You put the method in your Activity. 您将方法放在“活动”中。 It needs to override the method in WebView. 它需要重写WebView中的方法。 For example: 例如:

    webview.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
          ...
        }

} }

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

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