Android的 - 退出一个web视图时,音频仍然继续播放 [英] Android - when exiting a WebView, the audio still keeps playing

查看:219
本文介绍了Android的 - 退出一个web视图时,音频仍然继续播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code的工作,并与它唯一的问题是,如果我离开的WebView,甚至关闭该应用程序,甚至是preSS电源的电话,从播客音频继续播放。

请问谁知道如何停止?

下面是code:

 公共类PodcastsActivity扩展BaseActivity //实现ActionBar.OnNavigationListener
{
    的WebView的WebView = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        //setTheme(R.style.Theme_Sherlock_Light);
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.podcasts);


        的WebView =新的WebView(本);
        webview.getSettings()setAppCacheEnabled(假)。
        webview.getSettings()setJavaScriptEnabled(真)。
        webview.setInitialScale(1);
        。webview.getSettings()setPluginState(PluginState.ON);

        webview.setWebViewClient(新WebViewClient(){
            @覆盖
            公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                view.loadUrl(URL);
                返回true;
            }
        });




        //webSettings.setBuiltInZoomControls(true);


        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(真正的);
        webSettings.setBuiltInZoomControls(真正的);
        //webSettings.getMediaPlaybackRequiresUserGesture();
        webSettings.setAllowContentAccess(真正的);
        webSettings.setEnableSmoothTransition(真正的);
        webSettings.setLoadsImagesAutomatically(真正的);
        webSettings.setLoadWithOverviewMode(真正的);
        webSettings.setSupportZoom(真正的);
        webSettings.setUseWideViewPort(真正的);

        的setContentView(web视图);
        webview.loadUrl("http://www.stitcher.com/podcast/entre$p$pneuronfirecom/entre$p$pneur-on-fire-tim-ferriss-other-incredible-entre$p$pneurs");

    }

//公共无效onBack pressed()
// {
//的Class.forName(融为一体。***。HTML5WebView)。GetMethod的(的onPause,(等级[])为空)。
//调用(html5WebView,(对象[])NULL);
//}


    @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件)
    {
        如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack())
        {
            webview.goBack();
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }

    @覆盖
    公共无效的onStop()
    {
       super.onStop();
       //你的code

       webview.clearView();
    }
 

解决方案

您应该通过打电话向的的WebView的的onPause() 和<一href="http://developer.android.com/reference/android/webkit/WebView.html#onResume%28%29"><$c$c>onResume()从活动的的onPause() onResume(),分别为。

  

暂停与该web视图和其相关联的任何额外的处理   关联的DOM,插件的JavaScript等。例如,如果此web视图   取屏幕外,这可以被称为以减少不必要的CPU或   网络流量。当此的WebView又是积极的,调用onResume()。


还有<一个href="http://developer.android.com/reference/android/webkit/WebView.html#pauseTimers%28%29"><$c$c>pauseTimers(),它会影响所有的WebViews你的应用程序中:

  

暂停所有的布局,解析,和JavaScript为所有WebViews定时器。   这是一个全球性的要求,而不是仅仅限于此的WebView。本   如果应用程序已被暂停可能是有用的。

I have this code which works and the only problem with it is that if I leave the WebView and even close the app, and even press power on the phone, the audio from the podcasts keeps playing.

Would anyone know how to stop that?

Here is the code:

public class PodcastsActivity extends BaseActivity //implements ActionBar.OnNavigationListener 
{    
    WebView webview = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        //setTheme(R.style.Theme_Sherlock_Light);
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.podcasts);


        webview = new WebView(this);
        webview.getSettings().setAppCacheEnabled(false);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setInitialScale(1);
        webview.getSettings().setPluginState(PluginState.ON);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });




        //webSettings.setBuiltInZoomControls(true);


        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        //webSettings.getMediaPlaybackRequiresUserGesture();
        webSettings.setAllowContentAccess(true);
        webSettings.setEnableSmoothTransition(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);

        setContentView(webview);
        webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");           

    }

//  public void onBackPressed ( )
//  {
//      Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
//      invoke(html5WebView, (Object[]) null);
//  }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) 
        {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }       

    @Override
    public void onStop()
    {
       super.onStop();
       // your code

       webview.clearView();
    }

解决方案

You should call through to the WebView's onPause() and onResume() from your Activity's onPause() and onResume(), respectively.

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


There's also pauseTimers(), which affects all of the WebViews within your application:

Pauses all layout, parsing, and JavaScript timers for all WebViews. This is a global requests, not restricted to just this WebView. This can be useful if the application has been paused.

这篇关于Android的 - 退出一个web视图时,音频仍然继续播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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