Search code examples
javaandroidibm-mobilefirstandroid-serviceandroid-syncadapter

IBM mobile first: sendActionToJS not working with background service in Android


In my Application, I am trying to make a sync adapter service in Android which will run in the background when the app is killed.

This service will call WL.getInstance().sendActionToJS() to send the control to js.

I am using:

WL.App.setKeepAliveInBackground(true);

method to keep the app alive in Background, using this method I am able to use the instance to WL even though app is killed.

onSync.java:

    try {
        JSONObject data = new JSONObject();

        data.put("isConnected", true);

        data.put("connRes", "MOBILE");

        WL wl = WL.getInstance();

        if(wl!=null){

            L.e("WL is not null");

            wl.sendActionToJS("isConnected", data); 
        }
        else{
            L.e("WL is null");
        }


    } 

    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();




    }

main.js:

WL.App.setKeepAliveInBackground(true, options);

WL.App.addActionReceiver("MIActionReceiverId", function actionReceiver(received){

    console.log('MIActionReceiverId  .  '+JSON.stringify(received));



    else if(received.action == 'isConnected') {
        //Connectivity manager
        console.log('isConnected.  ');

        var isConnected = received.data.isConnected;
        console.log('isConnected.  '+isConnected);

    }




    }

WL.getInstance().sendActionToJS() method does nothing neither throwing any exception and addActionReceiver in main.js not receiving anything.

This is happening when app is killed and runnig the background rest of the time everything is working fine.


Solution

  • As mentioned in an earlier questions, there is no official support in the MobileFirst Android SDK to run in an Android service or otherwise, as such some aspects of the SDK that you try to use in this context will work, and some may not.

    In addition, I do not believe that this kind of using the sendAction API is even correct or possible...