2016-03-02 21 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

相關問題