我有一个应用程序可以在 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>