PhoneGap onDeviceReady
PhoneGap的Android应用如何监控一些硬件事件,比如:后退键、菜单键,网络连接状态的相关事件,见如下草记
首先,在某Page的PageCreate下,添加一个监听事件:
document.addEventListener("deviceready", onDeviceReady, false);
然后编写onDeviceReady事件代码:
function onDeviceReady() { console.log("DeviceReady!!!"); // Register the event listener document.addEventListener("backbutton", function(){ navigator.notification.confirm( '你确定退出程序吗?', function(button){ if(button == '1') { navigator.app.exitApp(); } },'退出','确定,取消'); }, false); document.addEventListener("menubutton", function(){ navigator.notification.confirm( '你确定退出程序吗?', function(button){ if(button == '1') { navigator.app.exitApp(); } },'退出','确定,取消'); }, false); document.addEventListener("online", onOnline, false); document.addEventListener("offline", onOffline, false); } function onOnline() { alert('Online!'); } function onOffline() { alert('Offline!'); }