允许所有“的市场://”从web视图打开谷歌Play商店中的链接 [英] Allow all 'market://' links from inside a webview to open Google Play store

查看:947
本文介绍了允许所有“的市场://”从web视图打开谷歌Play商店中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WebView是市场上一些环节://链接。当我的用户点击他们,这让他们有一个页面无法找到错误。

我怎样才能让开头的市场的各个环节://自动打开谷歌Play商店他们挖掘的时候?我想:

 最终意向意图=新的意图(android.intent.action.VIEW);
            intent.setData(Uri.parse(市场://细节ID =?));
            startActivity(意向);
        }
 

但是这似乎并没有做任何事情。我是pretty的新本,因此任何帮助将是AP preciated。此外,仅供参考,我不能改变市场://链接访问play.google.com自己。他们是从我的广告客户。

反正我有可以将其包含在这个code:

 公共布尔shouldOverrideUrlLoading(的WebView paramWebView,字符串中的paramString){
        如果(调试)
            Log.e(shouldOverride中的paramString);
        如果(Uri.parse(的paramString).getHost()=空&安培;!&安培;!(Uri.parse(的paramString).getHost()等于(market.android.com))及&安培;!(的paramString (!Uri.parse(的paramString).getHost;。载(facebook.com))及及(!Uri.parse(的paramString)。.getHost()包含(twitter.com))及&安培().equals(play.google.com))
                &功放;&安培; (!Uri.parse(的paramString).getHost()包含(bit.ly))及&安培; (!Uri.parse(的paramString).getHost()包含(plus.google.com))及&安培; (!Uri.parse(中的paramString).getHost()。包括(youtube.com))){
            如果(isAppOrGamePage(的paramString)){
                最终意向意图=新的意图(MainActivity.this,PageActivity.class);
                intent.putExtra(APP_URL中的paramString);
                startActivity(意向);
            } 其他
                返回false;
        } 其他 {
            最终意向意图=新的意图(android.intent.action.VIEW,Uri.parse(中的paramString));
            startActivity(意向);
        }

        返回true;
    }
}
 

解决方案

您可以决定怎么做看URL的方案,如果谷歌Play商店应用被安装,你可以在Play商店应用打开详细页,其他您可以显示应用程序的谷歌玩网页

  webView.setWebViewClient(新WebViewClient(){
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        如果(Uri.parse(URL).getScheme()。等于(市场)){
            尝试 {
                意向意图=新的意图(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(URL));
                活动主办=(活动)view.getContext();
                host.startActivity(意向);
                返回true;
            }赶上(ActivityNotFoundException E){
                //谷歌播放不安装应用程序,你可能要打开的应用程序商店的链接
                开放的我们的uri = Uri.parse(URL);
                view.loadUrl(http://play.google.com/store/apps/+ uri.getHost()+ uri.getQuery()?);
                返回false;
            }

        }
        返回false;
    }
});
 

I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.

How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:

final Intent intent = new Intent("android.intent.action.VIEW");
            intent.setData(Uri.parse("market://details?id="));
            startActivity(intent);
        }

but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.

Is there anyway I can include it in this code:

public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
        if (DEBUG)
            Log.e("shouldOverride", paramString);
        if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
                && (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
            if(isAppOrGamePage(paramString)){
                final Intent intent = new Intent(MainActivity.this, PageActivity.class);
                intent.putExtra("app_url", paramString);
                startActivity(intent);
            } else
                return false;
        } else {
            final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
            startActivity(intent);
        }

        return true;
    }
}

解决方案

You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getScheme().equals("market")) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                Activity host = (Activity) view.getContext();
                host.startActivity(intent);
                return true;
            } catch (ActivityNotFoundException e) {
                // Google Play app is not installed, you may want to open the app store link
                Uri uri = Uri.parse(url);
                view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
                return false;
            }

        }
        return false;
    }
});

这篇关于允许所有“的市场://”从web视图打开谷歌Play商店中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆