如何停止在Android中的WebView的YouTube视频播放? [英] How to stop youtube video playing in Android webview?

查看:499
本文介绍了如何停止在Android中的WebView的YouTube视频播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能阻止YouTube视频是发挥我的WebView?当我点击后退按钮的视频不会停止,而是继续在后台。

code:

  web视图=(web视图)findViewById(R.id.webview);
webView.getSettings()setPluginsEnabled(真)。
webView.getSettings()setJavaScriptEnabled(真)。
。webView.getSettings()setBuiltInZoomControls(假);
。webView.getSettings()setSupportZoom(假);
webView.loadData(myUrl,text / html的,UTF-8);
 

解决方案

请参阅以下职位约<一href="http://stackoverflow.com/questions/2040963/webview-threads-never-stop-webviewcorethread-cookiesyncmanager-http0-3">WebView线程不会停止

从本质上讲,你需要从你自己的活动的的onPause方法调用web视图的方法的onPause。

这个唯一的问题是,你不能直接调用web视图的的onPause方法,因为它是隐藏的。因此,你需要通过反射间接调用它。下面code应该让你开始设立自己的活动的的onPause方法:

  @覆盖
公共无效的onPause(){
    super.onPause();

    尝试 {
        的Class.forName(android.webkit.WebView)
                .getMethod(的onPause,(等级[])空)
                            .invoke(web视图,(对象[])NULL);

    }赶上(ClassNotFoundException的cnfe){
        ...
    }赶上(NoSuchMethodException nsme){
        ...
    }赶上(ITE的InvocationTargetException){
        ...
    }赶上(IllegalAccessException IAE){
        ...
    }
}
 

请注意,在try块上述变量'的WebView'是该类的私有实例变量,并分配给在活动的onCreate方法。

How can I stop a YouTube video which is played in my webview? When I click on the back button the video doesn't stop and instead continues in the background.

Code:

webView = (WebView) findViewById(R.id.webview); 
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false); 
webView.getSettings().setSupportZoom(false);
webView.loadData(myUrl,"text/html", "utf-8");

解决方案

See the following post about WebView threads never stopping

Essentially you'll need to call the WebView's onPause method from your own Activity's onPause method.

The only trick with this is that you cannot call the WebView's onPause method directly because it is hidden. Therefore you will need to call it indirectly via reflection. The following code should get you started on setting up your own Activity's onPause method:

@Override
public void onPause() {
    super.onPause();

    try {
        Class.forName("android.webkit.WebView")
                .getMethod("onPause", (Class[]) null)
                            .invoke(webview, (Object[]) null);

    } catch(ClassNotFoundException cnfe) {
        ...
    } catch(NoSuchMethodException nsme) {
        ...
    } catch(InvocationTargetException ite) {
        ...
    } catch (IllegalAccessException iae) {
        ...
    }
}

Note that the variable 'webview' in the try block above is a private instance variable for the class and is assigned to in the Activity's onCreate method.

这篇关于如何停止在Android中的WebView的YouTube视频播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆