![](/img/trans.png)
[英]How to select which URL should redirect to Android browser from WebView?
[英]Android, How to retrieve redirect URL from a WebView?
首先,我在網上沖浪,但沒有找到與我的情況相符的答案。 我想從 webview 中獲取特定的重定向 url,然后阻止 webview 打開它。 案例是用戶授權我的應用程序后的 Instagram 重定向 url。 怎么做? 這是我的代碼:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://api.instagram.com/oauth/authorize/?client_id=my_client_id&redirect_uri=my_redirect_uri&response_type=code");
在加載 url 之前,您必須為您的 webview 設置您的自定義 WebviewClient 覆蓋 shouldOverrideUrlLoading 方法。
mWebView.setWebViewClient(new WebViewClient()
{
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
return shouldOverrideUrlLoading(url);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
{
Uri uri = request.getUrl();
return shouldOverrideUrlLoading(uri.toString());
}
private boolean shouldOverrideUrlLoading(final String url)
{
Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
// Here put your code
return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
}
});
mWebView.loadUrl("Your URL");
在 webview 中查看用於處理重定向 url 和無需下載即可打開 PDF 的示例代碼。 https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500
您應該覆蓋 shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
if (url.indexOf('api.instagram.com') > -1) //check if that's a url you want to load internally
{
webView.loadUrl(url);
return true;
}
else
{
return false; //Let the system handle it
}
}
});
最后感謝 Udi I 對我的問題的回答,稍加改動,我設法找到了問題的解決方案。 這是對我有用的代碼:
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
return (url.indexOf("some part of my redirect uri") > -1);
}
});
webView.loadUrl(myUrl); //myUrl is the initial url.
使用上面的代碼,如果有任何包含重定向uri的url,webView將不會加載它。 否則,webView 將加載它。 還要感謝 Kamil Kaminski。
只需為您的WebView
設置WebViewClient
,並覆蓋WebViewClient
shouldOverrideUrlLoading
。 在該方法中,您可以獲取您的 url,並決定您的WebView
是否應該遵循重定向 url。 下面是例子:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// here you can assign parameter "url" to some field in class
// return true if you want to block redirection, false otherwise
return true;
}
});
您可以比較 URL 字符串/特定單詞,並相應地采取行動 -
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (url.contains(".pdf")
openPDF(url); // handle pdf opening
if(url.startsWith("tel:")
callNumber(url); // handle call processing
...
}
只是為了添加更多,您可以防止 webview 顯示傳統的錯誤消息屏幕,或繞過已知錯誤 -
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == -10) {
System.out.println("Error occured: Error code: "
+ errorCode + "..Ignoring this error");
return;
}
else
{
// Show your custom screen to notify error has occured
}
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.