Oracle MAF: Run in background

person Bijeshfolder_openMAF, Oraclelocal_offeraccess_time September 17, 2015

There may often be a requirement for a mobile app to continue running in the background after the user moves on to another app. A sample usecase may be to sync data from an online resource – especially when a connection becomes available again. This may be more convenient to do when the user is not actively using the app.

Forntunately, someone has already built a Cordova plug-in that allows you to keep the app running in the background. Go here for the plugin – GitHub/katzer.

The plugin is pretty easy to use. It adds a global JavaScript object backgroundMode. To enable background mode, simply invoke the javascript function cordova.plugins.backgroundMode.enable()

Use backgroundMode.onactivate to be notified when the app is put into background mode. This is where you would initiate your background actions.

A sample AMX page that increments an applicationScope counter when in the background mode.





             
            function incrementCount()
            {
                //only perform actions if in background mode, else finish running.
                if(cordova.plugins.backgroundMode.isActive())
                {
                    console.log("*********** Activated");
                    var counter = 0;
                    adf.mf.api.getValue("#{applicationScope.counter}"
                                           , function(req, res) {
                                                counter = res[0]['value'];
                                                counter = counter + 1;
                                                adf.mf.api.setValue( 
                                                        {"name": "#{applicationScope.counter}"
                                                         ,"value": counter }
                                                       ,function() {}
                                                       ,function() {});
                                                setTimeout(incrementCount, 5000);
                                            }
                                           ,function(req, res) {}
                                       );   
                }                                                                
        }

       document.addEventListener(
            'deviceready'
           , function () {
                    // Android customization
                    cordova.plugins.backgroundMode.setDefaults({ text:'Doing heavy tasks.'});
                    // Enable background mode
                    cordova.plugins.backgroundMode.enable();

                    // Called when background mode has been activated
                    cordova.plugins.backgroundMode.onactivate = function () {
                        setTimeout(incrementCount, 5000);
                    };           
                    adf.mf.api.setValue( {"name": "#{applicationScope.counter}", 
                                            "value": 0 }, 
                                            function() {}, 
                                            function() {});
                    console.log("*********** Initialised");    
                    adf.mf.api.getValue("#{applicationScope.counter}", 
                                    function(req, res) {
                                        console.log("Got Value:" + res[0]['value']);
                                    }, function(req, res) {}
                    );                                                    
               }
           , false);                

    
]]> 















Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>