在Android应用中控制硬件后退按钮

时间:2015-12-04 06:50:53

标签: back-button mobile-application

我正在制作Andriod应用。当应用程序启动时,它会显示一个加载页面,然后显示一个选择页面。我希望当我按下我的选择上的移动后退按钮时,应该从用户确认“你确定退出应用程序”。   我试试这段代码,但它不能正常工作

document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener('backbutton', function(event){
    event.preventDefault(); // EDIT
    navigator.app.exitApp(); // exit the app
});

1 个答案:

答案 0 :(得分:0)

I have tried this code and its working.

document.addEventListener("deviceready", onDeviceReady, false);     function onDeviceReady() {     document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button  }

function onBackKeyDown(e) {     e.preventDefault();     navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No");     // Prompt the user with the choice  }

function onConfirm(button) {     if(button==2){//If User selected No, then we just do nothing         return;     }else{         navigator.app.exitApp();// Otherwise we quit the app.     } }