backbutton确认退出app android + phonegap + jquery

如何在Phonegap在线系统中使用jQuery Mobile处理Android中的物理后退按钮? 我想显示确认退出应用程序(是 – 否)。 我尝试了很多方法但没有任何工作。

试试这个:

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. } } 

当第一次单击设置exitApp = true时,第二次单击后退按钮将退出应用程序。 但是设置一个Interval将exitApp的状态更改为false。 所以当在1秒内点击两次按钮时,将退出应用程序。

 document.addEventListener('deviceready', function() { var exitApp = false, intval = setInterval(function (){exitApp = false;}, 1000); document.addEventListener("backbutton", function (e){ e.preventDefault(); if (exitApp) { clearInterval(intval) (navigator.app && navigator.app.exitApp()) || (device && device.exitApp()) } else { exitApp = true history.back(1); } }, false); }, false); 

// Devicereadyfunction

 document.addEventListener('deviceready', function() { document.addEventListener("backbutton", ShowExitDialog, false); }, false); 

//按下后退按钮时的对话框

  function ShowExitDialog() { navigator.notification.confirm( ("Do you want to Exit?"), // message alertexit, // callback 'My APp', // title 'YES,NO' // buttonName ); } 

//调用退出函数

  function alertexit(button){ if(button=="1" || button==1) { device.exitApp(); } } 

不确定是否更改了phonegap或者除了Suhas和geet的解决方案之外什么只对我有用。 (但是肯定给了我所需要的东西,谢谢!)后退按钮基本上被打破了。

以下是使它适合我的调整:

加载应用程序后执行以下操作:

  document.addEventListener("backbutton", onBackKeyDown, false);//hijack the backbutton function onBackKeyDown(e){ var page = $.mobile.activePage.attr('id'); xStat.rec("back button clicked from page:" + page); if (page == 'menuPage'){//are you on the 'root page' from which phonegap will exit? e.preventDefault(); $.mobile.changePage('#aboutToExitAppPage'); } else { window.history.back();//restore normal back button functionality } } //somewhere else in your code for the "aboutToExit app" page $('#aboutToExitAppPage').on('pageinit', function(){ $(this).find('#exitApp').on('click', function(){ navigator.app.exitApp();//quit the app. }); }); 

和HTML

 

About to exit app

尝试这个//当设备就绪时

 document.addEventListener('deviceready', function() { document.addEventListener("clickBackbutton", ExitDialogPrompt, false); }, false); function ExitDialogPrompt() { navigator.notification.confirm( ("Do you want to Exit?"), // message prompt, // callback 'Your title', // title 'YES,NO' // button Name ); } function alertexit(button){ if(button=="0" || button==1) { navigator.app.exitApp(); } }