简体   繁体   中英

How To Link To An Android App From Android Webview

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?

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

See Handling Page Navigation section 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;
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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