h5移动端,监听手机返回键,两次退出app

2023-10-24  本文已影响0人  喜欢走弯路的人

h5移动端,监听手机返回键,两次退出app

在index.html文件的script中加入以下内容

<script>

document.addEventListener('plusready', function () {

          var first = null;

          var webview = plus.webview.currentWebview();

          plus.key.addEventListener('backbutton', function () {

              webview.canBack(function (e) {

                  if (e.canBack) {

                    webview.back(); //这里不建议修改自己跳转的路径 

                  } else {

                    //首次按键,提示‘再按一次退出应用’ 

                    if (!first) {

                      first = new Date().getTime(); //获取第一次点击的时间戳 

                      plus.nativeUI.toast("再按一次退出应用", {

                        duration: 'short'

                      }); //通过H5+ API 调用Android 上的toast 提示框 

                      setTimeout(function () {

                        first = null;

                      }, 1000);

                    } else {

                        // 获取第二次点击的时间戳, 两次之差 小于 1000ms 说明1s点击了两次,

                      if (new Date().getTime() - first < 1000) { 

                        plus.runtime.quit(); //退出应用 

              }

            }

          }

        })

      });

    });

</script>

上一篇 下一篇

猜你喜欢

热点阅读