如何暂停Flash内容在Android的WebView当我的行为是不可见的? [英] How do I pause Flash content in an Android WebView when my activity isn't visible?

查看:285
本文介绍了如何暂停Flash内容在Android的WebView当我的行为是不可见的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用使用web视图来显示Flash内容在我的活动。一切工作pretty的很好,但是当用户点击Home键把活动放到后台,Flash内容继续运行(声不断播放等)

我注意到,无论是普通的Andr​​oid浏览器,海豚浏览器似乎避免这个问题,并妥善暂停Flash内容时,浏览活动被置于后台。

在理想情况下,我想一个解决方案,杀死的WebView完全如果活动结束,否则暂停它(基本上复制了浏览器的默认行为)

下面是一个简单的测试,我放在一起加载一个游戏Kongregate的有一些背景音乐:

 公共类BrowserTest延伸活动{
    私人的WebView mWebView;

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);

            mWebView =新的WebView(本);
            mWebView.getSettings()setJavaScriptEnabled(真)。
            mWebView.getSettings()setPluginsEnabled(真)。

            mWebView.loadUrl(http://m.kongregate.com/games/Jiggmin/the-game-of-disorientation-mobile);
            的setContentView(mWebView);
        }

        @覆盖
        保护无效的onPause(){
            super.onPause();

            mWebView.pauseTimers();
            如果(isFinishing()){
                mWebView.loadUrl(有关:空白);
                的setContentView(新的FrameLayout(本));
            }
        }

        @覆盖
        保护无效onResume(){
            super.onResume();
            mWebView.resumeTimers();
        }
    }
 

我看了一下为股票浏览器最新的来源,它似乎是在做类似的事情(调用pauseTimers / resumeTimers),但我担心的code我一直在寻找的是过时的,因为它被调用函数也似乎不存在了。

我没有验证调用pauseTimers正在用它更新一个计数器一个简单的JavaScript的setInterval测试。有没有别的明显,我应该尝试在有关窗口或视图管理?

本文档移动Flash播放器说:

  

Flash播放器也将自动暂停播放SWF它不是在视图或前台应用程序,例如接收到来电或闹钟响起,降低CPU占用率,电池使用和内存使用情况时。

这似乎在这两个股票的浏览器,海豚浏览器可以正常使用,但不是在我的应用程序。任何想法/解决方案将大大AP preciated!

更新:这是我们最终加入到我们的活动得到这个工作的功能。我们把它与该活动的的onPause功能的onPause和onResume活动的onResume功能:

 私人无效callHiddenWebViewMethod(字符串名称){
    如果(mWebView!= NULL){
        尝试 {
            方法方法= WebView.class.getMethod(名称);
            method.invoke(mWebView);
        }赶上(NoSuchMethodException E){
            Log.error(没有这样的方法:+姓名,e);
        }赶上(IllegalAccessException E){
            Log.error(非法访问:+姓名,e);
        }赶上(的InvocationTargetException E){
            Log.error(调用目标异常:+姓名,e);
        }
    }
}
 

解决方案

我不明白,你看到 BrowserActivity 或任何呼吁 pauseTimers()。我看到的onPause() BrowserActivity 最终路线的onPause()的WebView 的>。然而,的onPause()的WebView @hide 注释,所以是不是SDK的一部分。即兴,这似乎像在onPause()是你想要的,给出的注释:

  

调用此暂停任何额外的   处理与该视图关联   和其相关联的   DOM /插件/ JavaScript的/等。对于   例如,如果视图被取   屏幕外,这可以叫   减少不必要的CPU和/或网络   交通。当视图再次   积极的,调用onResume()。

请注意所提到的插件,这是不是在 pauseTimers提到的()文档。

我会尝试使用反射来看看的onPause()(以及后来的 onResume(),当你想要的东西再次启动)做什么,你所需要的。如果是这样,请张贴一个问题 b.android.com 此事,要求他们对这些方法(或等值)添加到SDK 。您的要求似乎像这将是一个常见的​​一种,和解决方案,去过去的SDK是不是一个很好的解决方案。

I am experimenting with using a WebView to display Flash content inside my activity. Everything is working pretty well, but when the user hits the home key to put the activity into the background, the Flash content keeps running (sound keeps playing, etc)

I have noticed that both the stock Android browser and Dolphin Browser seem to avoid this problem, and properly pause the Flash content when the browsing activity is put into the background.

Ideally I would like a solution that kills the WebView completely if the activity is finishing, but pauses it otherwise (basically copying the default behavior of the browser)

Here is a simple test I put together that loads a game on Kongregate which has some background music:

    public class BrowserTest extends Activity {
    private WebView mWebView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mWebView = new WebView(this);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setPluginsEnabled(true);

            mWebView.loadUrl("http://m.kongregate.com/games/Jiggmin/the-game-of-disorientation-mobile");
            setContentView(mWebView);
        }

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

            mWebView.pauseTimers();
            if(isFinishing()){
                mWebView.loadUrl("about:blank");
                setContentView(new FrameLayout(this));
            }
        }

        @Override
        protected void onResume(){
            super.onResume();
            mWebView.resumeTimers();
        }
    }

I took a look at the latest source for the stock browser, and it seems to be doing something similar (calling pauseTimers/resumeTimers), although I fear the code I have been looking at is out of date, because it is calling functions that don't seem to exist anymore.

I did verify that the call to pauseTimers is working by testing with a simple JavaScript setInterval which updates a counter. Is there something else obvious that I should be trying in regard to Window or View management?

The documentation for the mobile Flash player says:

Flash Player will also automatically pause SWF playback it is not in view or the foreground application, for example when a call is received or alarm goes off, to reduce CPU utilization, battery usage and memory usage.

This seems to be working perfectly in both the stock browser and Dolphin Browser, but not in my app. Any ideas/solutions would be greatly appreciated!

Update: Here is the function we ended up adding to our activity to get this to work. We call it with "onPause" in the activity's onPause function and "onResume" in the activity's onResume function:

private void callHiddenWebViewMethod(String name){
    if( mWebView != null ){
        try {
            Method method = WebView.class.getMethod(name);
            method.invoke(mWebView);
        } catch (NoSuchMethodException e) {
            Log.error("No such method: " + name, e);
        } catch (IllegalAccessException e) {
            Log.error("Illegal Access: " + name, e);
        } catch (InvocationTargetException e) {
            Log.error("Invocation Target Exception: " + name, e);
        }
    }
}

解决方案

I don't see where you see that BrowserActivity or anything is calling pauseTimers(). I do see where onPause() for BrowserActivity eventually routes to onPause() of WebView. However, onPause() of WebView has the @hide annotation and so is not part of the SDK. Off the cuff, it would seem like onPause() is what you want, given the comment:

Call this to pause any extra processing associated with this view and its associated DOM/plugins/javascript/etc. For example, if the view is taken offscreen, this could be called to reduce unnecessary CPU and/or network traffic. When the view is again "active", call onResume().

Note the reference to "plugins", which is not mentioned in the pauseTimers() documentation.

I'd try using reflection to see if onPause() (and later onResume(), when you want stuff to start up again) does what you need. If it does, please post an issue to b.android.com about this, asking them to add these methods (or equivalents) to the SDK. Your requirement seems like it will be a common one, and a solution that goes past the SDK is not a good solution.

这篇关于如何暂停Flash内容在Android的WebView当我的行为是不可见的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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