0

We are trying to make a sync adapter service in Android which will run in the background when the app is killed.

This service will fetch some data from JsonStore and will sync up with the server.

Code:

 try {

        URI adapterPath = new URI("/dummy/adapter");

        WLResourceRequest request = new WLResourceRequest(adapterPath,WLResourceRequest.POST);

        request.send(new AdapterListener(new CallbackAdapter() {

            @Override
            public void onFetch(String response) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(String error) {
                // TODO Auto-generated method stub

            }
        }));

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

Problems:

  1. When we try to run service in a different process, we get an error at line ( WLResourceRequest request = new WLResourceRequest(adapterPath,WLResourceRequest.POST);) that WL.getInstance should be called after WL.createInstance but we can not create WL instance in service because it needs an instance of ACTIVITY.

  2. When we try to run service in the same process, in which the app is currently running, everything works fine untile app is running but if we kill the app same things happen which are happening at point 1.

Questions:

  1. Is there a way we can create WL instance in service.

  2. Is there a way we can let WL instance initialized forever, even though user kills the app.

  3. Is there a way we can let our app run forever with WL instance initialized forever.

Nishant Tanwar
  • 383
  • 4
  • 8
  • 18

2 Answers2

1

I got it working, all you need to add

WL.App.setKeepAliveInBackground(true);

into the js file and WL instance will work with sync adapters and services in Android.

Nishant Tanwar
  • 383
  • 4
  • 8
  • 18
0

Running the MobileFirst Android SDK in an Android Service is currently not supported. There is an open feature request for this ability, so please feel free to add your vote if you can want to make this happen. Search here: https://mobilefirstplatform.ibmcloud.com/help/

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • Ok, but what about this https://www.ibm.com/support/knowledgecenter/SSHS8R_7.1.0/com.ibm.worklight.dev.doc/devref/t_keeping_app_running_in_background.html? – Nishant Tanwar Nov 14 '16 at 08:00