音乐网站上不会后退按钮停止 [英] Music on website won't stop with back button

查看:162
本文介绍了音乐网站上不会后退按钮停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用的WebView的应用程序,但该网站已在背景音乐。我有code时后退按钮是pssed将回到previous页面,直到它可以回去了,并会返回主菜单$ P $。问题是,当它返回到主菜单,音乐不会停止,我需要它停下来。对不起只是一个初学者。任何帮助将真棒!

code:

  @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果(event.getAction()== KeyEvent.ACTION_DOWN){
            开关(键code)
            {
            案例KeyEvent.KEY code_BACK:
                如果(myWebView.canGoBack()==真){
                    myWebView.goBack();
                }其他{
                    this.finish();
                }
                返回true;
            }        }
        返回super.onKeyDown(键code,事件);
    }

XML

 <的WebView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / web视图
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =FILL_PARENT/>


解决方案

您的XML更改为:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /根
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的WebView
       机器人:ID =@ + ID / web视图
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent/>< / LinearLayout中>

获取你的活动你的LinearLayout(只是在你有你的WebView同一个地方):

 的LinearLayout根;

(随后的onCreate())

 根=(的LinearLayout)findViewById(R.id.root);

然后

  @覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(event.getAction()== KeyEvent.ACTION_DOWN){
        开关(键code){
        案例KeyEvent.KEY code_BACK:
            如果(myWebView.canGoBack()){
                myWebView.goBack();
            }
            其他{
                root.removeView(myWebView); //< -
                myWebView.removeAllViews(); //< - 添加这些行
                myWebView.destroy(); //< -
                this.finish();
            }
            返回true;
        }
    }
    返回super.onKeyDown(键code,事件);
}

I'm working on an app with a WebView but the website has music in the background. I have code when the back button is pressed will go back to previous page until it can go back anymore and will go back to main menu. Problem is that when it goes back to main menu, the music does not stop and I need it to stop. Sorry only a beginner. Any help would be awesome!

Code:

       @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(myWebView.canGoBack() == true){
                    myWebView.goBack();
                }else{
                    this.finish();
                }
                return true;
            }

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

Xml

 <WebView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/webView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

解决方案

Change your XML to:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView 
       android:id="@+id/webView"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</LinearLayout>

Get your LinearLayout in your activity (just in the same place you got your WebView):

LinearLayout root;

(then in onCreate())

root = (LinearLayout) findViewById(R.id.root);

Then

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if(myWebView.canGoBack()) {
                myWebView.goBack();
            }
            else {
                root.removeView(myWebView); // <- 
                myWebView.removeAllViews(); // <- add these lines
                myWebView.destroy();        // <-
                this.finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

这篇关于音乐网站上不会后退按钮停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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