我想从Facebook下载视频,我在Webview
加载Facebook。
这就是我在片段中设置Webview
的方式:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
webView = new WebView(getActivity());
webView.getSettings().setJavaScriptEnabled(true);
webView.setInitialScale(50);
webView.getSettings().setUseWideViewPort(true);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.loadUrl("http://m.facebook.com");
webView.setWebViewClient(new CustomWebViewClient());
return webView;
}
因此,为了从浏览器下载视频,我拦截了所有请求,并通过CustomWebViewClient查看链接包含mp4的天气:
CustomWebViewClinet :
public class CustomWebViewClient extends WebViewClient {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, final String url) {
if (url.contains("mp4")) {
Log.e(TAG, "shouldInterceptRequest: got mp4\n" + url);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showToast("Got mp4");
//Download Logic Here with "url"
}
});
return null;
}
return super.shouldInterceptRequest(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
现在的问题是,每当我点击视频时,我都会在shouldInterceptRequest()
获取我的mp4网址,但在Webview
中,它开始播放我不想要的Webview
播放器中的视频。想要阻止在Webview
中播放视频。我只想在不播放视频的情况下下载视频
正如你在Pic1中看到的那样,点击它时不会在webview中播放视频,它会显示要下载的对话框,但在我的情况下,在Pic2中,当对话框出现时,它开始在webview中播放视频