Run Process in Background

Hello All,

I was wondering if it is possible using corona to run a simple process in the background. Like send your newly updated GPS coordinates to the server every time it has been updated or every 10 minutes? I am assuming this will require native code. Any ballparks in terms of time and searching this would take to build for some one not familiar with Objective-C or Java?

Hi.  When you say background do you mean the app is not running as the current app, but sleeping/hibernating?

If you mean the app is running as the current active app and you simply want periodic work to be done, you can achieve that will no special coding.  Just use a timer.

As far as having a Corona app doing work while it isn’t active… I’m not sure you can, but if you can it will very-likely take an Enterprise license to make that type of change.

PS - I’ll be glad to be wrong on this and am looking forward to more answers from the community.

Apple supports a limited number of background modes including playing background music, some GPS features, Newsstand downloads, etc.  Some people have gotten this to work.  But the official answer from the Engineers is “This is unsupported in Corona SDK.  It may not work in future releases. And it doesn’t work on Android with Corona SDK at all”.  That said, you can take your chances, but no complaining when you update to a later release and your app breaks. 

This is unsupported.

That’s a real bummer.  I just lost my iPad and the “Find my iPhone” app is useless.  I can’t remember where or when I last used the thing and “Find my iPhone” won’t tell you that.  It simply says, “Device if offline.”   I would love to make an app that emails me the iPad’s last known location every few minutes, but it would have to operate in the background.

Hi.  When you say background do you mean the app is not running as the current app, but sleeping/hibernating?

If you mean the app is running as the current active app and you simply want periodic work to be done, you can achieve that will no special coding.  Just use a timer.

As far as having a Corona app doing work while it isn’t active… I’m not sure you can, but if you can it will very-likely take an Enterprise license to make that type of change.

PS - I’ll be glad to be wrong on this and am looking forward to more answers from the community.

Apple supports a limited number of background modes including playing background music, some GPS features, Newsstand downloads, etc.  Some people have gotten this to work.  But the official answer from the Engineers is “This is unsupported in Corona SDK.  It may not work in future releases. And it doesn’t work on Android with Corona SDK at all”.  That said, you can take your chances, but no complaining when you update to a later release and your app breaks. 

This is unsupported.

That’s a real bummer.  I just lost my iPad and the “Find my iPhone” app is useless.  I can’t remember where or when I last used the thing and “Find my iPhone” won’t tell you that.  It simply says, “Device if offline.”   I would love to make an app that emails me the iPad’s last known location every few minutes, but it would have to operate in the background.

I managed to get the audio continue to play in the background by adding the background audio capability to the app. Unfortunately the Corona code is stopped (no more code is called) so this is useless.

I added a simple performWithDelay in the Corona code and it stops triggering when the app is suspended (despite the sound is running to its end)… :frowning:

Rob, when you say “And it doesn’t work on Android with Corona SDK at all”… why? We’re using Corona Enterprise, so we run with some native layer. Do you mean the Corona SDK layer is suspended in background and no Corona code can be executed even if we use all the native requirement to keep the app in the background?

Enterprise aside (as I’m not sure what you can do there), and speaking strictly about Corona SDK… there are no controls you can set in your .lua code or in build.settings that controls how Android handles the backgrounding of your app.  On iOS you can put in pList entries and they make it into the device’s plist setting which allows some limited background functionality with iOS.   Those doing this on iOS are getting away with the fact that we let you add arbitrary things to the plist.  On Android we don’t let you add arbitrary things to the AndroidManifest.xml file. 

In Enterprise, you’re building a native app and if you know how to background an Android app, then you should, as long as you understand that we don’t test any of the Corona SDK api calls in a backgrounded fashion and we don’t support it. 

Rob

Thanks Rob.

Here what I tested with iOS / Corona Enterprise:

  • Adding the background mode

  • Run a function every second, displaying a trace (print…) with Corona

  • Play music and lock the phone

Result: The music still play but the timer stop running :(((

It’s like the Corona engine was paused…

I did not try to call a Corona Lua function from Objective C while the app is in background yet.

I’m developer here, so, let me bring some final clarification here.

The Corona runtime and its Lua scripts are tied to the UI.  So, when your app gets suspended, the Corona runtime gets suspended too.  Attempting to invoke anything in Corona’s Lua state while the app is in the background will likely run into some undefined behavior because all of Corona’s features/services such as timers and access to the display are suspended.  This is by design and we have zero plans on changing this.  This is because Corona’s primarily designed to do hardware accelerated rendering.

Now, since you are an Enterprise user, it is possible for you to do things in the background on Android via pure Java code outside of Corona.  You just can’t interact with Corona while in the background until your app is back in the foreground again.  If your backgrounded process needs to send any kind of feedback to Corona and its Lua scripts, then you’ll have to queue those operation and only execute them once your app is in the foreground again.  We do the same in our own internal code on Android.  You can leverage our “CoronaRuntimeTaskDispatcher” Java class if you want, which automatically queues your tasks and only executes them once your app is in the foreground and synced to the Corona runtime thread… or dumps them once the Corona runtime and its activity is destroyed.

Hey Joshua, this explains the behavior of my test :slight_smile:

It’s very nice to give us so detailed and transparent explanation. Now it’s clear.

Another possibility is to use CoronaCards.  Since CoronaCards does not control the entire application stack, it is up to the developer to pause/resume the CoronaView.  Just an idea…

Actually, you’ll run into issues with this with Corona Cards on Android too because it’s “CoronaRuntimeTaskDispatcher” (Corona’s equivalent to a Windows message pump) is tied to the rendering thread.  That is, its queued operations (such as your runtime Lua listeners) will only be executed before every “enterFrame” when rendering to the OpenGL surface, which will only happen if your app is in the foreground.

I managed to get the audio continue to play in the background by adding the background audio capability to the app. Unfortunately the Corona code is stopped (no more code is called) so this is useless.

I added a simple performWithDelay in the Corona code and it stops triggering when the app is suspended (despite the sound is running to its end)… :frowning:

Rob, when you say “And it doesn’t work on Android with Corona SDK at all”… why? We’re using Corona Enterprise, so we run with some native layer. Do you mean the Corona SDK layer is suspended in background and no Corona code can be executed even if we use all the native requirement to keep the app in the background?

Enterprise aside (as I’m not sure what you can do there), and speaking strictly about Corona SDK… there are no controls you can set in your .lua code or in build.settings that controls how Android handles the backgrounding of your app.  On iOS you can put in pList entries and they make it into the device’s plist setting which allows some limited background functionality with iOS.   Those doing this on iOS are getting away with the fact that we let you add arbitrary things to the plist.  On Android we don’t let you add arbitrary things to the AndroidManifest.xml file. 

In Enterprise, you’re building a native app and if you know how to background an Android app, then you should, as long as you understand that we don’t test any of the Corona SDK api calls in a backgrounded fashion and we don’t support it. 

Rob

Thanks Rob.

Here what I tested with iOS / Corona Enterprise:

  • Adding the background mode

  • Run a function every second, displaying a trace (print…) with Corona

  • Play music and lock the phone

Result: The music still play but the timer stop running :(((

It’s like the Corona engine was paused…

I did not try to call a Corona Lua function from Objective C while the app is in background yet.

I’m developer here, so, let me bring some final clarification here.

The Corona runtime and its Lua scripts are tied to the UI.  So, when your app gets suspended, the Corona runtime gets suspended too.  Attempting to invoke anything in Corona’s Lua state while the app is in the background will likely run into some undefined behavior because all of Corona’s features/services such as timers and access to the display are suspended.  This is by design and we have zero plans on changing this.  This is because Corona’s primarily designed to do hardware accelerated rendering.

Now, since you are an Enterprise user, it is possible for you to do things in the background on Android via pure Java code outside of Corona.  You just can’t interact with Corona while in the background until your app is back in the foreground again.  If your backgrounded process needs to send any kind of feedback to Corona and its Lua scripts, then you’ll have to queue those operation and only execute them once your app is in the foreground again.  We do the same in our own internal code on Android.  You can leverage our “CoronaRuntimeTaskDispatcher” Java class if you want, which automatically queues your tasks and only executes them once your app is in the foreground and synced to the Corona runtime thread… or dumps them once the Corona runtime and its activity is destroyed.