IBM Worklight - How to use Worklight in a backgrou

2019-08-27 20:49发布

问题:

I have an application that should run continuously even after the user presses the back button. It has to send some data using POST method to my database in a remote server for every half an hour. This should happen even after the user has pressed the back button and the app should only stop when the mobile is switched off.

While I have set up the HTTP Adapter to send POST data and also an HTML file to call the adapter procedures on launch, I dont know how to make it background or which code to run to keep sending the POST data every half an hour.

回答1:

It sounds like you are running on Android.

  1. The expected behavior in Android is that pressing the Back button QUITS the application.
  2. Moving to the background is performed by pressing the Home button.

So, I don't think you should do this when pressing the Back button... or maybe you should at least present the user the choice of either quitting or moving to the background (you can override the Back button by using WL.App.overrideBackButton).

That said, I am not familiar with a programmatic method to move to the background instead of quitting (maybe there is an existing Cordova plug-in or another way that does this).

The other way to accomplish this is by using an Android Background Service, however, Worklight does not have support in its Native API to do this.

As for what happens when the Worklight application is in the background, I have never tried it myself, so I cannot be certain at all, but try using WL.Client.setHeartBeatInterval to keep alive the connection to the Worklight Server and write some logic that sends the adapter requests. See if that would work for you...


I have found this: How to create plugin in phonegap to run the application in background?
Maybe you'll be able to massage it into your Worklight project (in case the heartbeat approach above does not work).

More information on running in the background:
Android:

  • http://developer.android.com/training/basics/activity-lifecycle/index.html
  • http://developer.android.com/reference/android/app/Activity.html

iOS:

  • http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html


回答2:

Override the Back button by using WL.App.overrideBackButton., remove the exit function in that method and leave it blank (if you want the background process not to exit when clicked)