Android – 退出WebView时,音频仍然播放

前端之家收集整理的这篇文章主要介绍了Android – 退出WebView时,音频仍然播放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码,它的唯一的问题是,如果我离开WebView,甚至关闭该应用程序,甚至按下电源,播客的音频继续播放.

有人会知道如何阻止吗?

这是代码

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();
    }

解决方法

您应该分别从Activity的onPause()和onResume()调用 WebView’s onPause()onResume().

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().

还有pauseTimers(),它影响了您的应用程序中的所有WebViews:

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.

原文链接:https://www.f2er.com/android/312024.html

猜你在找的Android相关文章