-1

您好,我正在开发 android 应用程序,并且我有一个列表项,每个itel 在 webview 上显示 url。但是当我在 webview 上打开 youtube 视频时,它无法显示。

这是我的活动代码

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class Slider extends Activity {
SlideView scrollView;

ExpandableListAdapter menuAdapter;
View menu;
View app;
Button btnSlide;
static boolean menuOut = false;
boolean isScan = false;
Handler handler = new Handler();
int btnWidth;
ArrayList<WebAddress> address = new ArrayList<WebAddress>();
WebView webView;
ProgressDialog mProgress;
boolean loadingFinished = true;
boolean redirect = false;
AlertDialog.Builder alert;
boolean isWebHistory = false;


ArrayList<String> listParentHeader;
HashMap<String, ArrayList<WebAddress>> listChildHeader;

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    if( Build.VERSION.SDK_INT >= 9){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

      // webView.getSettings().setPluginState(PluginState.ON);
        StrictMode.setThreadPolicy(policy); 
    }

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (SlideView) inflater.inflate(R.layout.screen_scroll_with_list_menu, null);
    setContentView(scrollView);

    final Stack stack=new Stack();
    menu = inflater.inflate(R.layout.horz_scroll_menu, null);
    app = inflater.inflate(R.layout.screen_facebook_slider, null);
    webView =(WebView) app.findViewById(R.id.webView);
    ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

// 地址 = Config.createAddress(); // menuAdapter = new MenuAdapter(this,R.layout.link,address);

    listParentHeader    =   Config.createParentHeader();
    listChildHeader     =   Config.createChildHeader();

    menuAdapter = new ExpandableListAdapter(this, listParentHeader, listChildHeader);
    ExpandableListView listView = (ExpandableListView) menu.findViewById(R.id.list);
    //ViewUtils.initListView(this, listView, "Menu ", 8, android.R.layout.simple_list_item_1);
    listView.setAdapter(menuAdapter);


    webView.setVisibility(0);
    webView.getSettings().setJavaScriptEnabled(true);


    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webView.getSettings().setUserAgentString("Android App");
    webView.setInitialScale(30); 
    webView.getSettings().setLoadWithOverviewMode(true ); 
    webView.getSettings().setUseWideViewPort(true); 
    webView.getSettings().setPluginsEnabled(true);
   // webView.getSettings().setPluginState(PluginState.ON);
    // force web view to open inside application
    webView.setWebChromeClient(new WebChromeClient());
    //webView.setWebViewClient(new MyWebViewClient());

   // webView.loadUrl("http://youtube.com/fortvalleystate1895");

    openURL("http://www.fvsu.edu/");


    listView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
              isWebHistory = true;

            menuOut = true;
            scrollWebviw(scrollView, menu);
            mProgress = ProgressDialog.show(Slider.this, "Loading", "Please wait for a moment...");
            openURL(((WebAddress)listChildHeader.get(listParentHeader.get(groupPosition))
                    .get(childPosition)).url);
            webView.requestFocus(View.FOCUS_DOWN);
            menuOut = false;
            webView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                        break;
                }
                return false;
                }
            });

            return false;
        }
    });


    btnSlide = (Button) tabBar.findViewById(R.id.BtnSlide);

    btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

    final View[] children = new View[] { menu, app };

    // Scroll to app (view[1]) when layout finished.
    int scrollToViewIdx = 1;

    scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
}



 /**
 * Helper for examples with a HSV that should be scrolled by a menu View's width.
 */
static class ClickListenerForScrolling implements OnClickListener {
    SlideView scrollView;
    View menu;
    /**
     * Menu must NOT be out/shown to start with.
     */
    //boolean menuOut = false;

    public ClickListenerForScrolling(SlideView scrollView, View menu) {
        super();
        this.scrollView = scrollView;
        this.menu = menu;
    }

    @Override
    public void onClick(View v) {
        Context context = menu.getContext();

        int menuWidth = menu.getMeasuredWidth();

        // Ensure menu is visible
        menu.setVisibility(View.VISIBLE);

        if (!menuOut) {
            // Scroll to 0 to reveal menu
            Log.d("===slide==","Scroll to right");
            Log.d("===clicked==","clicked");
            int left =20;
            scrollView.smoothScrollTo(left, 0);
        } else {
            // Scroll to menuWidth so menu isn't on screen.
            Log.d("===slide==","Scroll to left");
            Log.d("===clicked==","clicked");
            int left = menuWidth;
            scrollView.smoothScrollTo(left, 0);
        }
        menuOut = !menuOut;
    }
}

/**
 * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
 * showing.
 */
static class SizeCallbackForMenu implements SizeCallback {
    int btnWidth;
    View btnSlide;

    public SizeCallbackForMenu(View btnSlide) {
        super();
        this.btnSlide = btnSlide;
    }

    @Override
    public void onGlobalLayout() {
        btnWidth = btnSlide.getMeasuredWidth();
        System.out.println("btnWidth=" + btnWidth);
    }

    @Override
    public void getViewSize(int idx, int w, int h, int[] dims) {
        dims[0] = w;
        dims[1] = h;
        final int menuIdx = 0;
        if (idx == menuIdx) {
            dims[0] = w - btnWidth;
        }
    }
}


private void openURL(String url) {

    webView.loadUrl(url);

}

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
     // TODO Auto-generated method stub
     super.onPageStarted(view, url, favicon);
     loadingFinished = false;
     menuOut = false;
     //mProgress.show();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         if (!loadingFinished) {
              redirect = true;
           }

        loadingFinished = false;
        view.loadUrl(url);
        return true;
    }

    // when finish loading page
    public void onPageFinished(WebView view, String url) {
        if(!redirect){
              loadingFinished = true;
           }

           if(loadingFinished && !redirect){
               if(null !=mProgress) {
                   if(mProgress.isShowing()) {
                        mProgress.dismiss();
                        menuOut = false;
                    }
                }

           } else{
              redirect = false; 
           }
    }
}

//scroll the page and open the webview
private void scrollWebviw(SlideView scrollView, View menu) {
     Context context = menu.getContext();

     int menuWidth = menu.getMeasuredWidth();

     // Ensure menu is visible
     menu.setVisibility(View.VISIBLE);

     if (!menuOut) {
         // Scroll to 0 to reveal menu
        Log.d("===slide==","Scroll to right");
         int left = 0;
         scrollView.smoothScrollTo(left, 0);
     } else {
         // Scroll to menuWidth so menu isn't on screen.
        Log.d("===slide==","Scroll to left");
         int left = menuWidth;
         scrollView.smoothScrollTo(left, 0);

     }
     menuOut = false;
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webView.canGoBack() == true){
                    webView.goBack();
            }else if(isWebHistory && webView.canGoBack() == false){
                isWebHistory = false;
                Intent menu = new Intent(Slider.this, Slider.class);
                startActivity(menu);
                webView.clearHistory();

            }else{
                webView.clearCache(true);
                moveTaskToBack(true);
                Slider.this.finish();
            }
            return true;
        }

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


@Override
protected void onResume() {
    super.onResume();

}
@Override
public void onConfigurationChanged(final Configuration newConfig)
{
    // Ignore orientation change to keep activity from restarting
    super.onConfigurationChanged(newConfig);
}

}

这是获取 url 的类

public class Config {

static ArrayList<WebAddress> address = null;

static ArrayList<String> listParentHeader   =   null;

static HashMap<String, ArrayList<WebAddress>> listChildHeader   =   null;

public static ArrayList<String>  createParentHeader(){
    listParentHeader    =   new ArrayList<String>();
    listParentHeader.add("President Message");
    listParentHeader.add("Student Information");
    listParentHeader.add("FVSU Web Mail");
    listParentHeader.add("FVSU News");
    listParentHeader.add("Tickets Office");
    listParentHeader.add("FVSU Social Media");
    listParentHeader.add("Map");
    return listParentHeader;
}

public static HashMap<String, ArrayList<WebAddress>>  createChildHeader(){

    listChildHeader =   new HashMap<String, ArrayList<WebAddress>>();
    ArrayList<WebAddress> temp  =   new ArrayList<WebAddress>();
    temp.add(new WebAddress("president message", "http://m.youtube.com/watch?v=41Mh3iKN3eM"));
    temp.add(new WebAddress("Donate to FVSU", "http://www.fvsu.edu/give-online"));
    listChildHeader.put("President Message", temp);

    ArrayList<WebAddress> temp1 =   new ArrayList<WebAddress>();
    temp1.add(new WebAddress("Academics","http://www.fvsu.edu/academics" ));
    temp1.add(new WebAddress("Apply to FVSU","https://secure.gacollege411.org/Applications/USG_Common_App_Short/introduction.asp?application_id=1922" ));
    temp1.add(new WebAddress("Financial Aid","http://www.fvsu.edu/financial_aid" ));
    temp1.add(new WebAddress("Banner Web Login / Registration","https://fvsu.gabest.usg.edu/pls/B330/twbkwbis.P_GenMenu?name=homepage" ));
    temp1.add(new WebAddress("FVSU D2L","https://fvsu.view.usg.edu" ));
    listChildHeader.put("Student Information", temp1);

    ArrayList<WebAddress> temp2 =   new ArrayList<WebAddress>();
    temp2.add(new WebAddress("Faculty & Staff","https://webmail.fvsu.edu/owa/auth/logon.aspx?replaceCurrent=1&url=https%3a%2f%2fwebmail.fvsu.edu%2fowa%2f"));
    temp2.add(new WebAddress("Students","https://login.microsoftonline.com/login.srf?wa=wsignin1.0&rpsnv=2&ct=1383771248&rver=6.1.6206.0&wp=MBI_KEY&wreply=https:%2F%2Fwww.outlook.com%2Fowa%2F&id=260563&whr=wildcat.fvsu.edu&CBCXT=out"));
    listChildHeader.put("FVSU Web Mail", temp2);

    ArrayList<WebAddress> temp3 =   new ArrayList<WebAddress>();
    temp3.add(new WebAddress("FVSU Review","http://review.fvsu.edu"));
    temp3.add(new WebAddress("Sports","http://www.fvsusports.com"));
    listChildHeader.put("FVSU News", temp3);

    ArrayList<WebAddress> temp4 =   new ArrayList<WebAddress>();
    temp4.add(new WebAddress("Tickets Office","http://www.wildcatticketoffice.com"));
    listChildHeader.put("Tickets Office", temp4);

    ArrayList<WebAddress> temp5 =   new ArrayList<WebAddress>();
    temp5.add(new WebAddress("Facebook","https://m.facebook.com/fvsuwildcats"));
    temp5.add(new WebAddress("Twitter","https://twitter.com/fvsu"));
    temp5.add(new WebAddress("Youtube","http://youtube.com/fortvalleystate1895"));
    listChildHeader.put("FVSU Social Media", temp5);

    ArrayList<WebAddress> temp6 =   new ArrayList<WebAddress>();
    temp6.add(new WebAddress("Google Map","https://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=102899107517133380392.000490f1fae51c974e90d&ll=32.541857,-83.894434&spn=0.018089,0.02296&z=15&iwloc=000490f2097bca46ea46b&source=embed"));
    listChildHeader.put("Map", temp6);


    return listChildHeader;
}

}

任何有这个解决方案的请告诉我

4

1 回答 1

1

添加webview.setWebChromeClient(new WebChromeClient());并为您的视频启用插件添加:

if (Build.VERSION.SDK_INT < 8) {
        webview.getSettings().setPluginsEnabled(true);
    } else {
        webview.getSettings().setPluginState(PluginState.ON);
    }

这对我有用。希望这会有所帮助。

于 2013-11-08T03:09:04.850 回答