js怎么监听安卓返回键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js怎么监听安卓返回键相关的知识,希望对你有一定的参考价值。

代码如下:

    <script> 

            // =========================PhoneGap================================== 

            // 等待加载PhoneGap 

            document.addEventListener("deviceready", onDeviceReady, false); 

            // PhoneGap加载完毕 

            function onDeviceReady()  

                    // 按钮事件 

                    document.addEventListener("backbutton", eventBackButton, false); // 返回键 

               

         

          

        // 返回键 

        function eventBackButton()  

             if($(".classobj").is(":visible")) 

                         alert('再点击一次退出!'); 

                                document.removeEventListener("backbutton", eventBackButton, false); // 注销返回键 

                                document.addEventListener("backbutton", exitApp, false);//绑定退出事件 

                                // 3秒后重新注册 

                                var intervalID = window.setInterval(function()  

                                        window.clearInterval(intervalID); 

                                        document.removeEventListener("backbutton", exitApp, false); // 注销返回键 

                                        document.addEventListener("backbutton", eventBackButton, false); // 返回键 

                                , 3000); 

            else  

              //navigator.app.backHistory(); 

             

             

                           

         

          

        function exitApp() 

                navigator.app.exitApp(); 

         

    </script>

   

参考技术A 热心网友

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

<script>
// =========================PhoneGap==================================
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady()
// 按钮事件
document.addEventListener("backbutton", eventBackButton, false); // 返回键



// 返回键
function eventBackButton()
if($(".classobj").is(":visible"))
alert('再点击一次退出!');
document.removeEventListener("backbutton", eventBackButton, false); // 注销返回键
document.addEventListener("backbutton", exitApp, false);//绑定退出事件
// 3秒后重新注册
var intervalID = window.setInterval(function()
window.clearInterval(intervalID);
document.removeEventListener("backbutton", exitApp, false); // 注销返回键
document.addEventListener("backbutton", eventBackButton, false); // 返回键
, 3000);
else
//navigator.app.backHistory();





function exitApp()
navigator.app.exitApp();

</script>
参考技术B 得使用对象,js交汇 。os90安卓网为您回答希望采纳。 参考技术C <script> 
        // =========================PhoneGap================================== 
        // 等待加载PhoneGap 
        document.addEventListener("deviceready", onDeviceReady, false); 
        // PhoneGap加载完毕 
        function onDeviceReady()  
                // 按钮事件 
                document.addEventListener("backbutton", eventBackButton, false); // 返回键 
              
         
         
        // 返回键 
        function eventBackButton()  
             if($(".classobj").is(":visible")) 
                         alert('再点击一次退出!'); 
                                document.removeEventListener("backbutton", eventBackButton, false); // 注销返回键 
                                document.addEventListener("backbutton", exitApp, false);//绑定退出事件 
                                // 3秒后重新注册 
                                var intervalID = window.setInterval(function()  
                                        window.clearInterval(intervalID); 
                                        document.removeEventListener("backbutton", exitApp, false); // 注销返回键 
                                        document.addEventListener("backbutton", eventBackButton, false); // 返回键 
                                , 3000); 
            else  
              //navigator.app.backHistory(); 
            
             
                          
         
         
        function exitApp() 
                navigator.app.exitApp(); 
         
    </script>

本回答被提问者和网友采纳

以上是关于js怎么监听安卓返回键的主要内容,如果未能解决你的问题,请参考以下文章

vue的app监听返回键

【VUE】监听页面返回或移动端物理返回按钮键

H5监听手机返回按键写法(基于H5+)

vue项目监听安卓手机物理返回键

微信小程序怎么确定是息屏返回还是Home键操作?

JS监听手机返回键