Search code examples
androidibm-mobilefirstandroid-serviceandroid-syncadapter

Using IBM mobile first Android sdk in Background service when the app is killed.


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.


Solution

  • 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.