window.plugins undefined with phonegap build on some android device !?
tontione opened this issue · 16 comments
I try to use this plugin in my phonegapbuild app.
the phonegap version is 3.3.0
i added the correct line on my confix.xml :
<gap:plugin name="nl.x-services.plugins.insomnia" />
and this js call :
window.plugins.insomnia.keepAwake();
on some android devices (android 4.4.2) it works fine, but with others (android 4.3) i have an error
"window.plugins" is undefined.
What's the matter ?
Hi,
Your comment is missing some code, but I get the idea.
Are you waiting for the deviceready event to fire before calling the plugin?
Do we need to call the KeepAwake method before deviceready event is fired?
As I have done it inside the deviceready event handler method (Tested on Samsung Note 3), I don't get any errors and the app keeps working fine but screen dims after few seconds.
No, after. Can you share your code with me?
Hi,
Thank you for helping.
I have pasted below the js code for the initialise function.
I am using adobe build system to build the app. Please let me know if I have missed something.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function (id) {
window.plugins.insomnia.keepAwake();
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
try
{
navigator.geolocation.watchPosition(onSuccess, onFailure, {
frequency: 1000, maximumAge: 30000,
timeout: 5000,
enableHighAccuracy: false
});
function onSuccess(position) {
//on Success.
}
function onFailure() {
// on Failure
}
}
catch (err) {
// On Error
}
console.log('Received Event: ' + id);
}
};
Hi,
I have the same issue. I have tried:
<div onclick="window.plugins.insomnia.keepAwake()">keep awake</div>
and
document.addEventListener("deviceready", readytostart(), false);
function readytostart(){
window.plugins.insomnia.keepAwake();
}
Both of them dont work.
@chin123 the latter is incorrect, you need to do document.addEventListener("deviceready", readytostart, false);
(without the ()
)
Also, please add a JavaScript catch all error handler to see if something else is causing the problem:
window.onerror = function(a,b,c) {
alert(a); // etc
}
@EddyVerbruggen I did that now:
document.addEventListener("deviceready", gameready, false);
function gameready(){
window.onerror = function(a,b,c) {
alert(a); // etc
alert(b);
alert(c);
}
window.plugins.insomnia.keepAwake();
}
It doesn't work and no errors are appearing. Also, the prevent screen from sleeping permission doesnt appear when i try to install my app.This is on android.
That errorhandler needs to be moved outside the gameready
function.
Btw, which Android device/version is this?
I did that and it still gave no errors.This is android 5.1.1 on a nexus 7 2013.
I think it is a problem with my device and lollipop.I will reboot the device and check again.
No,the problem still persists.
OK, I just tested on my 5.1.1 device: all is fine.
So let's see where your problem is..
You don't by any chance have the Content-Security-Policy
metatag enable I hope, as the Cordova starter project doesn't allow inline script execution.
If not, please have a look at the Android log via adb logcat
when device is connected to your computer. Or use the 'catlog' app (find it in Google Play). Perhaps the log will give you a good hint.
If all else fails, email me your Android project.
Thanks for your help.It seems the catlog app requires root. I am not doing anything big , just testing out phonegap. Can you please tell me the code which you added so that the plugin works? If you want to, you can see my phonegap app repo with the insomnia plugin removed as of now : https://github.com/chin123/dodge-asteroids
Hmm, that repo isn't really useful with the plugin removed, is it :)
I just created a fresh project with the cordova CLI (are you using that, or build.phonegap.com)?
cordova create insomniatest
cd insomniatest
cordova platform add android
cordova plugin add https://github.com/EddyVerbruggen/Insomnia-PhoneGap-Plugin
cordova prepare
Then remove the csp metatag from index.html and add
<script>
document.addEventListener("deviceready", gameready, false);
function gameready(){
window.plugins.insomnia.keepAwake();
}
</script>
Sorry for not mentioning. I am using phonegap build.
Please re-enable Insomnia in your github project and let's continue the discussion over there.