我在loadDataWithBaseURL
中使用WebView
显示了一些博客的JSON内容。此博客的许多文章都嵌入了youtube / vimeo视频。
目前,所有视频都在网页视图中播放。
我想要实现的是,当用户点击播放视频时,他应该弹出"完成动作使用"浏览器或YouTube应用。简而言之,我不希望视频在app的webview中播放。
我发现每个人都试图避开这个弹出窗口的问题..不要'知道我为什么没有得到它。
这是我的代码
public class DetailFragment extends Fragment {
String strTitle = null;
String strURL = null;
String strContent = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
strTitle = getArguments().getString("title");
strURL = getArguments().getString("url");
strContent = getArguments().getString("content");
String contToDisplay = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body><style type='text/css'> img{width:80%;} .wp-image-42223{display:none;}</style>" + strContent+"</body></html>";
TextView title = (TextView) view.findViewById(R.id.title);
WebView desc = (WebView) view.findViewById(R.id.desc);
WebSettings ws = desc.getSettings();
ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
ws.setJavaScriptEnabled(true);
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
ws.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
title.setText(strTitle);
// desc.loadData(contToDisplay, "text/html; charset=UTF-8", null);
desc.loadDataWithBaseURL("about:blank",strContent,"text/html", "UTF-8", null);
return view;
}
}
任何使视频加载到浏览器或YouTube应用中的解决方案都会有所帮助。
注意
我在webview中显示的内容是HTML格式,iframe是其中一个元素。以下是示例数据:
"<p>Some text</p>
<p><iframe src=\"https://www.youtube.com/embed/q7uBAtaK15I\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"></iframe> </p>
<p>some more text</p>
此整个内容显示在webview中。我想要实现的是当有人点击iframe时,应该提示他complete action using
,即不允许该人在webview中播放视频
答案 0 :(得分:1)
我设法使用正则表达式实现了这一目标。
这就是我做的事情
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
strTitle = getArguments().getString("title");
strURL = getArguments().getString("url");
strContent = getArguments().getString("content");
contToDisplay = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /><script type='text/javascript'></script></head><body><style type='text/css'> img{width:80%;} .wp-image-42223{display:none;} </style>" + strContent+"</body></html>";
TextView title = (TextView) view.findViewById(R.id.title);
TextView date = (TextView) view.findViewById(R.id.date);
TextView authorName = (TextView) view.findViewById(R.id.auth);
desc = (WebView) view.findViewById(R.id.desc);
setHasOptionsMenu(true);
WebSettings ws = desc.getSettings();
ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
ws.setJavaScriptEnabled(true);
ws.setLoadWithOverviewMode(true);
ws.setUseWideViewPort(true);
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
ws.setSupportMultipleWindows(true);
ws.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
title.setText(strTitle);
final String s = strContent;
final String regex = "(?<=<iframe src=\")[^\"]*";
final String regex2 = "<\\s*iframe[^>]*><\\s*/\\s*iframe>";
p = Pattern.compile(regex);
final Pattern p2 = Pattern.compile(regex2);
m = p.matcher(s);
m2 = p2.matcher(s);
evaluate(s,contToDisplay,this);
desc.loadData(value, "text/html; charset=UTF-8", null);
return view;
}
String evaluate(String token,String defaultValue, DetailFragment context){
value=null;
Matcher matcher=p.matcher(token);
if (matcher.find()) {
iframeURL=matcher.group();
String newURL = "<a href=\"" + iframeURL + "\">Watch the Video Here</a>";
value = m2.replaceAll(newURL);
}
if (value == null) {
value=defaultValue;
}
return value != null ? value : "";
}
可以帮助有类似问题的人。
答案 1 :(得分:0)
这可能会有所帮助。我们正在正确的原生应用中加载新的网址加载。
desc.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url)
{
return true;
}
});
答案 2 :(得分:0)
desc.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url)
{
if(url.contains("youtube.com")){
// Could be cleverer and use a regex return
return super.shouldOverrideUrlLoading(view, url);
}
return true;
});
答案 3 :(得分:0)
如果链接嵌入在iframe中(如标题所示),则必须将WebChromeClient设置为webview并覆盖onCreateWindow方法:
@Override
public boolean onCreateWindow (WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView targetWebView = new WebView(view.getContext());
targetWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//simple url matching
if (url.contains("youtube.com") == true) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
view.getContex().startActivity(i);
}
view.stopLoading();
}
});
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(targetWebView);
resultMsg.sendToTarget();
return true;
}
在您的网页视图的网页上,您必须设置:
settings.setSupportMultipleWindows(true);