2016-03-02 23 views
1

我在这里搜索,但几乎所有的问题都是相反的..现在我问; 我有一个适用于android studio的webview应用程序。它通过我的webview应用程序打开位于HTML页面中的所有URL。我希望它打开应用程序中的网址,而不是webview

但我想补充一些例外。例如,我想在默认的Google Play应用中使用https://play.google.com ....但不是我的webview应用。

摘要:应用程序的WebView必须打开通过应用程序本身的一些正常的网址,但通过本地其他应用程序的一些特殊的URL ...

我webviewclient代码是这样;

public class MyAppWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().endsWith("http://play.google.com")) { 

      return false; 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     view.getContext().startActivity(intent); 
     return true; 
    } 
} 
+0

调试过你的代码了吗?我猜“如果”的说法是错误的? – Devrim

回答

1

如文档here说:

如果你真的想要一个全面的网络浏览器,那么你可能想 调用一个URL意图浏览器应用程序,而不是显示 它与WebView。

例如:

Uri uri = Uri.parse("http://www.example.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

至于你的谷歌游戏的具体问题,你可以找出如何做到这一点的位置:How to open the Google Play Store directly from my Android application?

编辑


它可以拦截来自WebView和i的链接点击补充你自己的行为。从this answer摘自:

WebView yourWebView; // initialize it as always... 
// this is the funny part: 
yourWebView.setWebViewClient(yourWebClient); 

// somewhere on your code... 
WebViewClient yourWebClient = new WebViewClient(){ 
    // you tell the webclient you want to catch when a url is about to load 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url){ 
     return true; 
    } 
    // here you execute an action when the URL you want is about to load 
    @Override 
    public void onLoadResource(WebView view, String url){ 
     if(url.equals("http://cnn.com")){ 
      // do whatever you want 
     } 
    } 
} 
+0

我必须使用本地HTML网页...网址位于其中...所有网址都使用我的webview应用程序打开...这很好。但我只想要一个特殊的网址...我只想要谷歌播放链接打开它的应用程序..不是我的webview应用程序 – ali

+0

@ali - 请参阅我上面的编辑。 – NoChinDeluxe

0

返回FALSE在shouldOverrideUrlLoading表示当前的WebView处理URL。所以你的if语句必须改变:

public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (Uri.parse(url).getHost().equals("play.google.com")) { 
     // if the host is play.google.com, do not load the url to webView. Let it open with its app 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     view.getContext().startActivity(intent); 

     return true; 
    } 
    return false; 
} 
+0

我用这个,但同样...任何改变...我使用 loadUrl(“file:///android_asset/home.html”); 显示本地HTML文件...和谷歌播放网址位于它。但当我点击它,所有的网址都打开与webview ...我不想要这个,我想所有链接打开与web视图,但除了一个网址:谷歌播放网址必须打开本身的应用程序.. – ali

相关问题