2016-05-23 55 views
0

在我的應用程序中,我想將URL限制爲多個域。例如,如果我想將其限制在一個域,這個工程:Android:將webview限制爲多個網址

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if(Uri.parse(url).getHost().endsWith("stackoverflow.com")) { 
     return false; 
    } 

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

那麼,如何改變這一行:

if(Uri.parse(url).getHost().endsWith("stackoverflow.com")) 

白名單多個URL,如:

stackoverflow.com 
stackexchange.com 
google.com 
+0

我想添加的所有網址ArrayList和使用'如果(urlList.contains(Uri.parse(URL).getHost())){返回false}其他{//打開頁面}' –

回答

1

我認爲最快的方法是保留你的「白標」主機列表。

List<String> whiteHosts = Arrays.asList("stackoverflow.com", "stackexchange.com", "google.com"); 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     String host = Uri.parse(url).getHost(); 
    if(whiteHosts.contains(host) { 
     return false; 
    } 

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

非常感謝你很多,完美的作品! –