Android Device Buttons And PhoneGap/Cordova
I am setting up the Android Device Buttons to work with my app. According to the PG/Cordova documentation I should be able to just add an listener such as document.addEventListener
Solution 1:
First of all, ur function write into the console.
document.addEventListener("backbutton", function() {
console.log('Back Button Pressed.');
}, false);
You should set your device as debugging tools (, you will find below a quick hint how to do it, for more info go here )
- Connect your phone to the pc
- On your phone: check USB debugging; setting => Developer Option => USB debugging
- On your phone: check unknown sources; settings >> applications >> unknown sources = true
- add your device to the ADT (in your SDK)
- run the project directly from the pc to the mobile : right-click on the project => Run AS => choose your device
NOW on Back button Click, "Back Button Pressed." will be printed in the console of Eclipse.
If your device is not connected to the Computer or if your device is not set as debugging tools, Nothing will change.
In General case Use "alert()" in stead of "console.log()" if you are testing on the device without setting it as debugging tools.
Beside, dont use the native java, use the original documentation of Cordova, just add the following code into your javascript files:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
// whatever you want to do
alert('Back button Pressed');
}
then you should add "onLoad()" to the body tag:
<body onLoad="onLoad()">
Post a Comment for "Android Device Buttons And PhoneGap/Cordova"