Run Process in Background

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.

Hi Joshua,

So, this issue is a “Game Changer” concerning my app. If true I may need to use another platform to develop this particular app. So I did some creative thinking about this issue and I considered using Push Notifications. I went to the documentation concerning Push Notifications and discovered some text that “seemed” to contradict what you said in a previous post (I could be wrong).

So here is what I found in the documentation:

Corona SDK supports two types of notifications:  local  and  push. The purpose of both notification types is to notify the user about something — a message or an upcoming appointment, for example — when the application isn’t running in the foreground.

AND

How an app reacts to notifications depends on its state:

Not running  — the app has exited by design, user decision, reboot, etc. In this state, if the user interacts with the notification, the operating system will launch the app. Information about the notification event will be passed to the app using “launch args”. The app should watch for these and react accordingly, for example, show a “snooze” button for an alarm or open a native.newWebView() if it has a URL. Note, however, that if the user starts the app by tapping directly on its icon, the notification will not be presented.

Backgrounded  — the app is running but it’s not the active app. In this state, if the user interacts with the notification by swiping on the unlock screen while the notification is showing or tapping on it in the notification center, the operating system will bring the app to the foreground and make it active. At this time, the app will receive the notification event but  not  receive launch args.

Although I decided that using Push or Local Notifications will not be appropriate for this app (we can not require the user to repond to the notification every 60 seconds) the documentation suggests that Corona SDK code does run while “Backgrounded”

The app I am creating needs to update a Parse database every 60 seconds (user initiated) using a network request whether it is in the foreground or background.

Is this possible?

Thanks

The point I was making above is that Corona’s Lua runtime does *not* run in the background.  This means that your Lua scripts will never get invoked in the background.  Lua scripts are only invoked/running while your app is in the foreground.  This is because Corona is mostly designed to make rendering media/game apps easy, meaning it is heavily tied to an OpenGL surface.

Now, of course Corona’s services run in the background, but that’s an internal implementation detail.  For example, our notification system uses a BroadcastReceiver on Android that receives push and local notifications while the app is in the background.  It has to work this way because that is how we post notifications to the top status bar when the GCM intent has been received.  You just can’t receive those notifications in Lua until the app has been brought to the foreground when (1) the user taps on the notifications (2) the notification was received while the app was in the foreground.

That said, if you need to run code in the background, then you really have no choice but to write native code, because there is no nice cross-platform means of doing this.  You can use Corona Enterprise or Corona Cards to do this.

Thank Joshua for explaining that. It makes sense now. Also after a couple hours of testing on my Samsung it proves to be true. Darn it! Well it just goes to prove that no one tool is right for every job. I still consider Corona SDK as my “goto” tool for (most of) my mobile development efforts.

Thanks :slight_smile:

Hi Joshua,

So, this issue is a “Game Changer” concerning my app. If true I may need to use another platform to develop this particular app. So I did some creative thinking about this issue and I considered using Push Notifications. I went to the documentation concerning Push Notifications and discovered some text that “seemed” to contradict what you said in a previous post (I could be wrong).

So here is what I found in the documentation:

Corona SDK supports two types of notifications:  local  and  push. The purpose of both notification types is to notify the user about something — a message or an upcoming appointment, for example — when the application isn’t running in the foreground.

AND

How an app reacts to notifications depends on its state:

Not running  — the app has exited by design, user decision, reboot, etc. In this state, if the user interacts with the notification, the operating system will launch the app. Information about the notification event will be passed to the app using “launch args”. The app should watch for these and react accordingly, for example, show a “snooze” button for an alarm or open a native.newWebView() if it has a URL. Note, however, that if the user starts the app by tapping directly on its icon, the notification will not be presented.

Backgrounded  — the app is running but it’s not the active app. In this state, if the user interacts with the notification by swiping on the unlock screen while the notification is showing or tapping on it in the notification center, the operating system will bring the app to the foreground and make it active. At this time, the app will receive the notification event but  not  receive launch args.

Although I decided that using Push or Local Notifications will not be appropriate for this app (we can not require the user to repond to the notification every 60 seconds) the documentation suggests that Corona SDK code does run while “Backgrounded”

The app I am creating needs to update a Parse database every 60 seconds (user initiated) using a network request whether it is in the foreground or background.

Is this possible?

Thanks

The point I was making above is that Corona’s Lua runtime does *not* run in the background.  This means that your Lua scripts will never get invoked in the background.  Lua scripts are only invoked/running while your app is in the foreground.  This is because Corona is mostly designed to make rendering media/game apps easy, meaning it is heavily tied to an OpenGL surface.

Now, of course Corona’s services run in the background, but that’s an internal implementation detail.  For example, our notification system uses a BroadcastReceiver on Android that receives push and local notifications while the app is in the background.  It has to work this way because that is how we post notifications to the top status bar when the GCM intent has been received.  You just can’t receive those notifications in Lua until the app has been brought to the foreground when (1) the user taps on the notifications (2) the notification was received while the app was in the foreground.

That said, if you need to run code in the background, then you really have no choice but to write native code, because there is no nice cross-platform means of doing this.  You can use Corona Enterprise or Corona Cards to do this.

Thank Joshua for explaining that. It makes sense now. Also after a couple hours of testing on my Samsung it proves to be true. Darn it! Well it just goes to prove that no one tool is right for every job. I still consider Corona SDK as my “goto” tool for (most of) my mobile development efforts.

Thanks :slight_smile:

Can someone advise how to access the Corona activity main thread in Enterprise version? I have a game that needs to render a video but it looks like the Corona UI thread is not able to render the video. Thanks.

Have a look at Corona Enterprise sample project “SimpleLuaExtension”.  Particularly its “AsyncCallLuaFunction.java” file.  That example shows you how to use a Java “Runnable” class to invoke something on the main UI thread from the Corona thread.  It also shows you how to get back on to the Corona thread from the main UI thread (or any thread really) via our “CoronaRuntimeTaskDispatcher” class.  The Java code in that example is heavily commented/documented and I think it does a good job explaining how it works.

Here are some Java class/method documentation that will help you out as well…

   http://developer.android.com/reference/java/lang/Runnable.html

   http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/package-summary.html

Our Corona Enterprise bundle also includes an “ExtendingUI” sample project which shows you how to add native Android UI on top of our CoronaView.  It’s worth a look as well, but it doesn’t show you how to pass a Lua function to Java and have it called later… provided that’s something you need to do.

Can someone advise how to access the Corona activity main thread in Enterprise version? I have a game that needs to render a video but it looks like the Corona UI thread is not able to render the video. Thanks.

Have a look at Corona Enterprise sample project “SimpleLuaExtension”.  Particularly its “AsyncCallLuaFunction.java” file.  That example shows you how to use a Java “Runnable” class to invoke something on the main UI thread from the Corona thread.  It also shows you how to get back on to the Corona thread from the main UI thread (or any thread really) via our “CoronaRuntimeTaskDispatcher” class.  The Java code in that example is heavily commented/documented and I think it does a good job explaining how it works.

Here are some Java class/method documentation that will help you out as well…

   http://developer.android.com/reference/java/lang/Runnable.html

   http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

   http://docs.coronalabs.com/daily/native/android/html/com/ansca/corona/package-summary.html

Our Corona Enterprise bundle also includes an “ExtendingUI” sample project which shows you how to add native Android UI on top of our CoronaView.  It’s worth a look as well, but it doesn’t show you how to pass a Lua function to Java and have it called later… provided that’s something you need to do.