0

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

1 回答 1

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);
        }
    });
于 2013-06-25T23:54:16.163 回答