Android Webview help Needed. Please Help

  • Replies:0
Seth Erves
  • Forum posts: 1

Jul 8, 2012, 6:30:29 AM via Website

I have a app that loads a page in webview this page has random youtube links. I would like the youtube app to open when the user selects one of these links. I have tried to create an intent with no luck and I have searched the internet for days with no luck. Also the links will not necessarily be the same each time the page loads so they will need to be parsed. Can someone help me please? My code is below



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;



/** Called when the activity is first created. */

    @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>

Reply