h5移動(dòng)端挠轴,監(jiān)聽(tīng)手機(jī)返回鍵,兩次退出app
在index.html文件的script中加入以下內(nèi)容
<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(); //這里不建議修改自己跳轉(zhuǎn)的路徑?
? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? //首次按鍵耳幢,提示‘再按一次退出應(yīng)用’?
? ? ? ? ? ? ? ? ? ? if (!first) {
? ? ? ? ? ? ? ? ? ? ? first = new Date().getTime(); //獲取第一次點(diǎn)擊的時(shí)間戳?
? ? ? ? ? ? ? ? ? ? ? plus.nativeUI.toast("再按一次退出應(yīng)用", {
? ? ? ? ? ? ? ? ? ? ? ? duration: 'short'
? ? ? ? ? ? ? ? ? ? ? }); //通過(guò)H5+ API 調(diào)用Android 上的toast 提示框?
? ? ? ? ? ? ? ? ? ? ? setTimeout(function () {
? ? ? ? ? ? ? ? ? ? ? ? first = null;
? ? ? ? ? ? ? ? ? ? ? }, 1000);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? // 獲取第二次點(diǎn)擊的時(shí)間戳, 兩次之差 小于 1000ms 說(shuō)明1s點(diǎn)擊了兩次,
? ? ? ? ? ? ? ? ? ? ? if (new Date().getTime() - first < 1000) {?
? ? ? ? ? ? ? ? ? ? ? ? plus.runtime.quit(); //退出應(yīng)用?
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? })
? ? ? });
? ? });
</script>