开业电话:& mailto:Android WebViewClient 中的链接

发布于 2024-12-10 03:56:50 字数 1621 浏览 0 评论 0原文

在过去的几天里,我一直在寻找这个问题的答案,我可以理解。在尝试了我在网上看到的所有代码片段后,我仍然遇到困难。我对 android sdk 和 java 非常陌生,实际上这是我第一次编写 Android 应用程序。所以我的问题是,为什么我在单击 mailto 或 tel 链接时不断收到如此著名的错误“页面无法显示”?

这是我的代码:

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

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. 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 ?

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

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

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

发布评论

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

评论(1

來不及說愛妳 2024-12-17 03:56:50

您将该方法放入您的活动中。需要重写WebView中的方法。例如:

    webview.setWebViewClient(new WebViewClient() {

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

}

You put the method in your Activity. It needs to override the method in WebView. For example:

    webview.setWebViewClient(new WebViewClient() {

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

}

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