android - PhoneGap - OnDeviceReady method not getting called -
i developing first application in phonegap (android).
index.html
<!doctype html> <html> <head> <title>device properties example</title> <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> <script type="text/javascript" charset="utf-8"> alert('code: 1'); // wait cordova load // document.addeventlistener("deviceready", ondeviceready, false); alert('code: 2'); var watchid = null; alert('code: 3'); // cordova ready // function ondeviceready() { // update every 3 seconds alert('code: 4'); var options = { frequency: 3000 }; watchid = navigator.geolocation.watchposition(onsuccess, onerror, options); } // onsuccess geolocation // function onsuccess(position) { alert('code: 5'); var element = document.getelementbyid('geolocation'); element.innerhtml = 'latitude: ' + position.coords.latitude + '<br />' + 'longitude: ' + position.coords.longitude + '<br />' + '<hr />' + element.innerhtml; } // clear watch started earlier // function clearwatch() { alert('code: 6'); if (watchid != null) { navigator.geolocation.clearwatch(watchid); watchid = null; } } // onerror callback receives positionerror object // function onerror(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); } </script> </head> <body> <p id="geolocation">watching geolocation...</p> <button onclick="clearwatch();">clear watch</button> </body> </html> here ondeviceready method not getting called. kindly me understand missing.
i have added these permissions
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_wifi_state"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> in manifest.xml file.
follow way , should working.
<!doctype html> <html> <head> <title>cordova device ready example</title> <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> <script type="text/javascript" charset="utf-8"> // call ondeviceready when cordova loaded. // // @ point, document has loaded cordova-1.7.0.js has not. // when cordova loaded , talking native device, // call event `deviceready`. // function onload() { document.addeventlistener("deviceready", ondeviceready, false); } // cordova loaded , safe make calls cordova methods // function ondeviceready() { // safe use cordova api } </script> </head> <body onload="onload()"> </body> </html> also check android example folder in cordova 1.7 download.
Comments
Post a Comment