如何从 Android Webview 链接到 Android 应用程序

发布于 2024-12-23 01:35:31 字数 664 浏览 1 评论 0原文

目前我有一个 Android 应用程序,它基本上是一个加载网页的 Web 视图。在网页上,我尝试像这样链接到市场...

https://market.android.com/details?id=com.google.earth

http://market.android.com/details?id=com.google.earth

market://details?id=com.google.earth

第一个结果只是打开一个白色屏幕(它可能正在加载,但已经超过一分钟了)。

第二个结果表明页面已移动并有一个链接。如果您单击该链接,它将执行第一个链接的操作。

第三个结果表明页面可能暂时关闭。 (它将链接视为在线链接,而不是手机本身)

链接的外观如下...

echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";

请记住,我正在加载的网页正在使用 JQuery Mobile,并且我正在使用 php 回显该链接。

如何在网页上的网络视图中打开 Android Market 的链接?

Currently I have an Android app that is basically a web view loading a web page. On the web page I've tryed to link to the market like this...

https://market.android.com/details?id=com.google.earth

http://market.android.com/details?id=com.google.earth

market://details?id=com.google.earth

The first result just opens up a white screen (It may be loading, but it has been their for over a minute).

The second result says that the page has been moved and a link. If you click th link it does what the first one did.

The third result says that the page may be temporarily down. (It's treating the link like its online rather than in the phone itself)

Here is how the link looks...

echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";

Remember the web page I'm loading is using JQuery Mobile and I'm echoing the link with php.

How can I open a link to the Android Market in a webview on a web page?

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

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

发布评论

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

评论(2

昇り龍 2024-12-30 01:35:31

就我而言,我遇到了所描述的相同的空白/暂时移动的问题。我想使用 shouldOverrideUrlLoading ,以便在从我的页面到 google 再返回到我的页面的 oauth2 流程中不使用本机浏览器。我的 Android 应用程序正在使用自签名证书与 localhost/tomcat 进行通信。事实证明,由于证书不匹配,我需要调用继续:

webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i("DevAgentSocialMain", "URL: " + url);
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
        //super.onReceivedSslError(view, handler, error);
        handler.proceed();
    }
});

In my case, I was getting the same blank/moved temporarily trouble described. I wanted to use the shouldOverrideUrlLoading so that the native browser wasn't used in an oauth2 flow from my page to google and back to my page. My android app was talking to localhost/tomcat with a self-signed cert. Turned out I needed to call proceed because of cert mismatch:

webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i("DevAgentSocialMain", "URL: " + url);
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
        //super.onReceivedSslError(view, handler, error);
        handler.proceed();
    }
});
隐诗 2024-12-30 01:35:31

当用户单击 Web 视图中的链接时,您可以使用回调。


Android 开发者平台上的处理页面导航部分。

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);

然后你的回调

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

You can use a callback when a user clicks a link inside the webview.

See
Handling Page Navigationsection on the android developer platform.

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);

Then your callback

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文