2015-01-10 141 views
0

我有兩個活動的Android應用程序:其中一個是Youtube Webview,另一個是從鍵盤獲取字符串的主要活動。如何在WebView中搜索Youtube視頻?

當我開始Webview活動時,有什麼方法可以顯示在Youtube中搜索字符串的搜索嗎?

感謝

回答

0

是的,你可以在搜索的WebView &播放視頻

enter image description here

public class UniversalWebViewFragment extends Fragment { 

    private static final String GOOGLE_SERACH_URL = "https://www.google.com/search?q="; 

    private static final String YOUTUBE_SERACH_URL = "https://www.youtube.com/results?search_query=android"; 

    private WebView webView; 
    private FrameLayout customViewContainer; 
    private WebChromeClient.CustomViewCallback customViewCallback; 
    private View mCustomView; 
    private myWebChromeClient mWebChromeClient; 
    private myWebViewClient mWebViewClient; 

    public static UniversalWebViewFragment newInstance() { 

     return new UniversalWebViewFragment(); 
    } 

    public void searchOnGoogle(String key) { 
     webView.loadUrl(GOOGLE_SERACH_URL + key); 
    } 

    public void searchOnYoutube(String key) { 
     webView.loadUrl(YOUTUBE_SERACH_URL + key); 
    } 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.universal_web_view, 
       container, false); 

     customViewContainer = (FrameLayout) rootView 
       .findViewById(R.id.customViewContainer); 
     webView = (WebView) rootView.findViewById(R.id.webView); 

     mWebViewClient = new myWebViewClient(); 
     webView.setWebViewClient(mWebViewClient); 

     mWebChromeClient = new myWebChromeClient(); 
     webView.setWebChromeClient(mWebChromeClient); 
     webView.getSettings().setJavaScriptEnabled(true); 

     // Important for PayUMoney 
     webView.getSettings().setDomStorageEnabled(true); 

     webView.getSettings().setAppCacheEnabled(true); 
     webView.getSettings().setBuiltInZoomControls(true); 
     webView.getSettings().setSaveFormData(true); 
     webView.loadUrl(GOOGLE_SERACH_URL); 
     // webView.requestFocus(); 

     // Handle Back keyPress 
     rootView.setFocusableInTouchMode(true); 
     rootView.requestFocus(); 
     rootView.setOnKeyListener(new OnKeyListener() { 

      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 

       if (keyCode == KeyEvent.KEYCODE_BACK) { 

        return true; 

       } 
       // return super.onKeyDown(keyCode, event); 
       return true; 
      } 
     }); 

     return rootView; 
    } 

    public boolean inCustomView() { 
     return (mCustomView != null); 
    } 

    public void hideCustomView() { 
     mWebChromeClient.onHideCustomView(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); // To change body of overridden methods use File | 
          // Settings | File Templates. 
     webView.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); // To change body of overridden methods use File | 
          // Settings | File Templates. 
     webView.onResume(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); // To change body of overridden methods use File | 
         // Settings | File Templates. 
     if (inCustomView()) { 
      hideCustomView(); 
     } 
    } 

    class myWebChromeClient extends WebChromeClient { 
     private View mVideoProgressView; 

     @Override 
     public void onShowCustomView(View view, int requestedOrientation, 
       CustomViewCallback callback) { 
      onShowCustomView(view, callback); // To change body of overridden 
               // methods use File | Settings | 
               // File Templates. 
     } 

     @Override 
     public void onShowCustomView(View view, CustomViewCallback callback) { 

      // if a view already exists then immediately terminate the new one 
      if (mCustomView != null) { 
       callback.onCustomViewHidden(); 
       return; 
      } 
      mCustomView = view; 
      webView.setVisibility(View.GONE); 
      customViewContainer.setVisibility(View.VISIBLE); 
      customViewContainer.addView(view); 
      customViewCallback = callback; 
     } 

     @Override 
     public View getVideoLoadingProgressView() { 

      if (mVideoProgressView == null) { 
       LayoutInflater inflater = LayoutInflater.from(getActivity()); 
       mVideoProgressView = inflater.inflate(R.layout.video_progress, 
         null); 
      } 
      return mVideoProgressView; 
     } 

     @Override 
     public void onHideCustomView() { 
      super.onHideCustomView(); // To change body of overridden methods 
             // use File | Settings | File Templates. 
      if (mCustomView == null) 
       return; 

      webView.setVisibility(View.VISIBLE); 
      customViewContainer.setVisibility(View.GONE); 

      // Hide the custom view. 
      mCustomView.setVisibility(View.GONE); 

      // Remove the custom view from its container. 
      customViewContainer.removeView(mCustomView); 
      customViewCallback.onCustomViewHidden(); 

      mCustomView = null; 
     } 
    } 

    class myWebViewClient extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      return super.shouldOverrideUrlLoading(view, url); 
     } 

     private int webViewPreviousState; 

     private final int PAGE_STARTED = 0x1; 

     private final int PAGE_REDIRECTED = 0x2; 

     Dialog dialog = new Dialog(getActivity()); 

     /* 
     * (non-Javadoc) 
     * 
     * /* (non-Javadoc) 
     * 
     * @see 
     * android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, 
     * java.lang.String, android.graphics.Bitmap) 
     */ 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      webViewPreviousState = PAGE_STARTED; 

      if (dialog == null || !dialog.isShowing()) 
       dialog = ProgressDialog.show(getActivity(), "", 
         "Loading Please Wait", true, true, 
         new OnCancelListener() { 

          @Override 
          public void onCancel(DialogInterface dialog) { 
           // do something 
          } 
         }); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      if (webViewPreviousState == PAGE_STARTED) { 
       if (null != dialog) 
        dialog.dismiss(); 
       dialog = null; 
      } 

     } 
    } 

} 

這裏是佈局的YouTube

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     > 
    <WebView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/webView" 
      android:layout_gravity="center" 
      /> 
    <FrameLayout 
      android:id="@+id/customViewContainer" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:visibility="gone" 
      /> 
</LinearLayout> 

佈局FO [R YouTube的進步

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/progress_indicator" 
        android:orientation="vertical" 
        android:layout_centerInParent="true" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

     <ProgressBar android:id="@android:id/progress" 
        style="?android:attr/progressBarStyleLarge" 
        android:layout_gravity="center" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 

     <TextView android:paddingTop="5dip" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:text="loading" 
        android:textSize="14sp" 
        android:textColor="?android:attr/textColorPrimary"/> 
    </LinearLayout> 

使用方法如下

universalWebViewFragment = UniversalWebViewFragment.newInstance(); 
getFragmentManager().beginTransaction() 
     .add(R.id.frame_root, universalWebViewFragment).commit(); 

搜索視頻這樣

    universalWebViewFragment.searchOnYoutube(searchView 
          .getText().toString()); 

不要忘了補充許可

​​

這裏找到完整的項目https://github.com/hiteshsahu/UniversalWebPageLoader