For what it's worth. I finally got it to work.
But unfortunately not with PhoneGap. I used stock cordova and then added this library
https://github.com/katzer/cordova-plugin-background-mode by running
Code:
$ cordova plugin add cordova-plugin-background-mode
and copied the the PhoneGap project files over to the new cordova project. Been running for 3 hours and its still active.
You have to enable and send the app in to BG mode. Altough there is functionality to override the back button to send to BG mode instead of closing the app which I havent tested yet, I dont see anything for when you press the home button.
[EDIT]
Some sample code if anyone ever come across this post in future.
Start a new project ,
Code:
$ cordova create hellobackground com.example.hellobackground "Hello Background"
Code:
$cordova platform add android
Code:
$ cordova plugin add cordova-plugin-background-mode
Replace index.html and index.js contents with the below.
index.html
Code:
Enable BG Mode
Disable GB Mode
Check BG Mode
Push to BG
index.js
Code:
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() {
},
// 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) {
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;');
console.log('Received Event: ' + id);
}
};
//**********************************************************************************************
//BOF BUTTON DECLARAES
//**********************************************************************************************
document.getElementById("idCheckBGMode").addEventListener("click", funcCheckBGMode);
document.getElementById("idEnableBGMode").addEventListener("click", funcEnableBgMode);
document.getElementById("idDisableBGMode").addEventListener("click", funcDisableBgMode);
document.getElementById("idPushtoBG").addEventListener("click", funcPushtoBG);
//**********************************************************************************************
//EOF BUTTON DECLARAES
//**********************************************************************************************
//**********************************************************************************************
//BOF Functions
//**********************************************************************************************
function funcCheckBGMode()
{
var BGMode=cordova.plugins.backgroundMode.isActive();
alert(BGMode);
}
function funcEnableBgMode()
{
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.disableWebViewOptimizations();
}
function funcDisableBgMode()
{
cordova.plugins.backgroundMode.disable();
}
function funcPushtoBG()
{
cordova.plugins.backgroundMode.moveToBackground();
}
//**********************************************************************************************
//EOF Functions
//**********************************************************************************************
//**********************************************************************************************
//BOF Timers
//**********************************************************************************************
//**********************************************************************************************
//EOF Timers
//**********************************************************************************************