I have an application that I started to do with ionic. A priori will be compiled only for android. In this application the user will make some registrations, which will be saved locally and every time a service in the background should try to synchronize the database with a server (a post). I completed all the steps successfully and left that part of the background last.
I'm using the plugin cordova-plugin-background-mode to perform this task. However, even testing all possible combinations over and over again, I realized that although the application stays in the background and does what I need, at some point android ends the application (free memory for example). And this causes all background services to stop running until the application is opened and placed in the background again.
I have already created services that run in the background (services and broadcasts receivers) with java for native android, but the current application is relatively large and a change would now be impractical.
My code in the app.js file looks like this:
angular.module('starter', ['ionic', 'ngCordova', 'ngStorage', 'ionic-material'])
.run(function ($ionicPlatform, LocalizacaoService, $http) {
$ionicPlatform.registerBackButtonAction(function (e) {
e.preventDefault();
}, 1000);
$ionicPlatform.ready(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(false);
}
if (window.StatusBar) {
//StatusBar.styleDefault();
StatusBar.styleLightContent();
}
})
document.addEventListener('deviceready', function () {
cordova.plugins.backgroundMode.overrideBackButton();
cordova.plugins.backgroundMode.excludeFromTaskList();
//cordova.plugins.backgroundMode.configure({silent: true});
cordova.plugins.backgroundMode.setDefaults({
title: "My App",
text: "Something here",
});
// Enable background mode
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.isActive();
cordova.plugins.backgroundMode.on('activate', function () {
cordova.plugins.backgroundMode.disableWebViewOptimizations();
});
// Run when the device is ready
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
//localizacao a cada 5 min
//localiza, salva e envia tudo
setInterval(function () {
//executa uma tarefa
console.log("executou")
}, 10000);
//sinc de visitas a cada 30 min
setInterval(function () {
//executa outra
console.log('executou');
}, 18000);
}
}, false);
});
My questions are: is my plugin implementation wrong? or is there a possibility that after the android code is created, a service with native android be implemented in the code?