我有一个在webview中加载页面的应用程序此页面包含随机的youtube链接。我想在用户选择其中一个链接时打开youtube应用。我试图创造一个没有运气的意图,我已经在互联网上搜索了几天没有运气。每次页面加载时链接也不一定相同,因此需要对它们进行解析。有谁可以帮助我吗?我的代码在
之下import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Activity1 extends Activity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.beatdrop.us/blog");
webView.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// YouTube video link
if (url.startsWith("vnd.youtube:"))
{
int n = url.indexOf("?");
if (n > 0)
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(String.format("http://www.youtube.com/v/%s",
url.substring("vnd.youtube:".length(),n)))));
}
return (true);
}
return (false);
}
});
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="youtube.com" android:pathPrefix="/watch" />
<data android:scheme="https" android:host="youtube.com" android:pathPrefix="/watch" />
</intent-filter>
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="*.youtube.com" android:pathPrefix="/watch" />
<data android:scheme="https" android:host="*.youtube.com" android:pathPrefix="/watch" />
</intent-filter>
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/v/" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/e/" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/embed/" />
<data android:scheme="https" android:host="www.youtube.com" android:pathPrefix="/embed/" />
</intent-filter>
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="vnd.youtube" />
</intent-filter>
答案 0 :(得分:0)
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
@Override
public void onPageFinished(final WebView view, String url) {
String javascript = "javascript:" +
"var iframes = document.getElementsByTagName('iframe');" +
"for (var i = 0, l = iframes.length; i < l; i++) {" +
" var iframe = iframes[i]," +
" a = document.createElement('a');" +
" a.setAttribute('href', iframe.src);" +
" d = document.createElement('div');" +
" d.style.width = iframe.offsetWidth + 'px';" +
" d.style.height = iframe.offsetHeight + 'px';" +
" d.style.top = iframe.offsetTop + 'px';" +
" d.style.left = iframe.offsetLeft + 'px';" +
" d.style.position = 'absolute';" +
" d.style.opacity = '0';" +
" d.style.filter = 'alpha(opacity=0)';" +
" d.style.background = 'black';" +
" a.appendChild(d);" +
" iframe.offsetParent.appendChild(a);" +
"}";
view.loadUrl(javascript);
super.onPageFinished(view, url);
}
});