Cordova battery status event only fires on first page - android

I have written a small app in Cordova with the battery plugin (tried versions 0.2.11 to 0.2.13-dev). The html contains a href to a second page. The deviceready event is fired on the first and on the second page and the battery status eventlistener is added on both pages. But on the second page the battery status event is not fired anymore, when i go back to the first page, it is not fired anymore, too.
I tried on Android 4.1 and Android 4.4 devices.
It seems that the battery status event fires only on the initial page.
Does anyone have experienced the same? Is there eventually a solution or workaround for this?
Many thanks.

According to documentation http://docs.phonegap.com/en/edge/cordova_events_events.md.html#deviceready
I expect this event is only fired once the application is started - meaning the Cordova Javascript parts are loaded and in cache. I expect the Cordova Javascript from cache is used even when loading a second html page.
You may want to use the Javascript window.onload = function ... to execute something when the page has been loaded.

Related

Cordova InAppBrowser opens double window on Android

I have developed an app in Cordova 3.6 with latest version of InAppBrowser. Run on Android device (4.1.2 and others) when the user taps a link to open InAppBrowser, occasionally a double window seems to open up. The second of these windows doesn't close.
To check if it was something I had done in my app, I quickly created a default Cordova hello world app and added the standard window.open code with link like this
OPEN WINDOW
and basic testing on the device revealed the same thing was happening - occasionally fast or double tapping made a double window open up, one of which was uncloseable. Either from <300ms double tap, or from double taps where the browser is slow to launch.
This doesn't appear to happen on iOS.
Any help gratefully appreciated.
UPDATE
Part of the problem on my main app was functions declared in wrong place (onpagecreate) being fired multiple times. I put this here in case someone does anything similar...
However on the hello world app the problem still occasionally happens. I tried with and without Fastclick, but Fastclick didn't work properly (possible conflict with jQuery Mobile, arrghh).
your problem is likely caused by using the 'onclick' event to launch the childbrowser.
onclick has a built in 300ms timeout, which can make an app appear laggy and causes some of the issues above.
Use mouseup/mousedown instead, and inside the handler, disable the listener
OPEN WINDOW
function open(url, name) {
// deregister the onclick listener, insuring the callback resolves
window.open(url,name);
// register the listener
}

How do I reload the dom in jquery mobile app

Please I need somen to help me before I loose my mind.
I am developing a mobile app with jquery mobile 1.4. The problem is that when users edit a form content it saves in local storage but does not show unless I manually refresh the browser so I implemented the following script
//After the selected employee has been edited
localStorage.setItem("employees", JSON.stringify(employees)); //set the update values to localstorage
alert('Eployee updated Successfully');
$.mobile.changePage("#home_page");
To make the dom refresh so that I dont have to manually refresh I implemented this
window.location.reload('#employee_list_view_page');
return true;
Which works well but when I converted to android using phone gap, once it updates it brings a white screen and the app hangs (like crashing).
Please what other way can I refresh or reload the dom that is more effecient. Or is there a way I can set a timeout for the refresh to stop after some seconds so the blank screen stops.
Thanks
I think what you're after is here: http://api.jquerymobile.com/pagecontainer/
Something like:
$.mobile.pageContainer.pagecontainer("change","index.html",{reloadPage:true});
Unfortunately this is deprecated as of jQuery Mobile 1.4, but the new method reload is affected by a bug.
Have a try and keep an eye on this: https://github.com/jquery/jquery-mobile/issues/7406

Force login on jQuery Mobile/Phonegap app restart

I've got an app running jQuery 1.9.1, jQuery Mobile 1.4.2 and Phonegap 3.4.0.
It has a login screen so that when you first open the app, you input your info, it does an AJAX check and if all is well, stores your info and moves you past that page.
The ideal scenario, is that everytime the app is sent to the background (the user goes home or switches apps), and then restarts the app they have to login again.
Currently I am accomplishing this in iOS by setting the
<preference name="exit-on-suspend" value="true" />
preference in my config.xml. I am accomplishing the same thing in Android by using navigator.app.exitApp(); in a pause event listener.
This works well enough usually, but some older versions of android have a weird problem. While going home and restarting the app bring you to the login page, you can just hit the back button and it takes you to the last page you were on before you quit, and you are still logged in. I also have a function that destroys all user variables (rendering all pages useless) when the app pauses, but that also seems to be ignored. On older versions of Android (2.3 specifically), it seems that they are ignoring the pause event listener.
How do I fix that, or is there an all around better way of forcing login on restart?
Without knowing what other requirements you have or what behaviors you might expect from the home page, you could capture the back button event and prevent it from going anywhere.
//set a bit or note that you are on the login page
var onHome = true;
...
document.addEventListener("backbutton", backButtonTap, false);
function backButtonTap(e) {
if(onHome) {
e.preventDefault();
alert('Please Login');
}
}
Certainly not the only or perhaps the best way to handle it since the user appears to stay logged in, which you say is supposed to be wiped out, but it should at least prevent navigating away from the login page and going back.

How to catch back and home button in jQuery Mobile + PhoneGap

I have tried the majority of the solutions posted both here and on other sites. I still can't catch the back button on my PhoneGap + jQuery Mobile android app. Let's start with the basic one:
document.addEventListener("backbutton", backKeyDown, true);
function backKeyDown() {
alert('back button pressed');
}
That doesn't seem to be firing at all.
Another thing I want to do is catch when the home button is pressed. I want to make it so that the app gets killed when I press the home button, mainly for security purposes.
Can anybody help me with my above dilemma?
I'm running into the same issue. I haven't fully solved it yet, but the problem seems to be that calls to the app plugin, including the call to enable button events, aren't running.
I was able to get the backbutton event firing by enabling the native part of the event binding in the java code:
super.loadUrl("file:///android_asset/www/loading.html", 20000);
appView.bindButton(true);
Update:
I found the actual cause of the problem - a bug in the development version of cordova that is now fixed.
Usually updating cordova is the first thing I try, but a simple pull didn't work because the repo is now http://github.com/apache/cordova-android - the old incubator-cordova-android still exists, but is not being updated and contains a version of the code from before this bug was fixed.

Executing Javascript in background using Phonegap

I've created an application that will need to fire Javascript events every "X" minutes. I'm using phone gap build to deploy on Android/iPhone/Blackberry, but I'm of course stuck, as the Javascript timer doesn't fire in the background (when they minimize the app). I was wondering if Phonegap had some sort of ability to fire Javascript events while the app is running in the background.
Or is there ANY way to accomplish this easily across these major phones? I just need to fire an ajax call with the geolocation info from the phones.
Yes you can do it, there's a PhoneGap plugin that does exactly what you need:
PhoneGap + Background.js
https://github.com/jocull/phonegap-backgroundjs
Be careful configuring your config.xml
Currently, phone gap doesn't have this as an option. There are many requests out there for this sort of thing, but I think people are getting tripped up by the TOS for background events for iOS. Regardless, a phone gap plugin would do it as #Chase mentioned, but of course that requires native code writing.
I'm assuming you're wanting to push geo locations every x seconds/minutes so that your server knows where the phone is at any point in time (as long as the app is open or minimized)? That's one limitation of Loopt, is that it can't grab those locations in the background - hence the whole 'ping' action in the app.

Categories

Resources