How to keep executing a function when app is running background

I want to keep detecting user location even when app is running in background & if I found user on certain location I need to do something. For that I’ve written logic as following:


local function listener()

    

   – detect user location

   – if userLoc == requiredLoc then 

   --do something

end

timer.performWithDelay(3000, listener, -1)


This will work when app will be running in foreground but I need it to keep executing even in suspend mode.

Does run time listener work & call listener function even in suspend mode.


 local function locationHandler( event ) 

     

        DeviceLocation.lati = event.latitude

        DeviceLocation.longi = event.longitude

        print("DeviceLocation.lati ",DeviceLocation.lati)

        print("DeviceLocation.longi ",DeviceLocation.longi)

      

end


    Runtime:addEventListener( “location”, locationHandler )

Unfortunately no users ever reply on this kind of posts

Corona SDK apps cannot run in the background except in very specific and unsupported circumstances.

Rob

As Rob says, this kind of functionality is not officially supported.

For location events you could try setting UIBackgroundModes to include “location” in your build.settings.

Not sure if it will work as expected though…

I think the problem with location events is that it’s going to want to wake up the app to handle the event.  That means the Lua code will receive the event and with no UI in place (it’s backgrounded) unpredictable and likely bad things will happen.

Rob

Unfortunately no users ever reply on this kind of posts

Corona SDK apps cannot run in the background except in very specific and unsupported circumstances.

Rob

As Rob says, this kind of functionality is not officially supported.

For location events you could try setting UIBackgroundModes to include “location” in your build.settings.

Not sure if it will work as expected though…

I think the problem with location events is that it’s going to want to wake up the app to handle the event.  That means the Lua code will receive the event and with no UI in place (it’s backgrounded) unpredictable and likely bad things will happen.

Rob

Hi, I’m making an APP for a Driver Service agency that updates GPS coordinates every 1 minute to check driver position… This is not possibile with Corona SDK? Do you will add an api to make this in the future? I hate java…

@cruero, I don’t think that running apps in background will be introduced in the near future… Your probably going to need to stick with java…

Good Luck!

[quote name=“SonicX278” post=“307791” timestamp=“1443635996”]@cruero, I don’t think that running apps in background will be introduced in the near future… Your probably going to need to stick with java…   Good Luck![/quote] thanks for your reply… i’m discontented for the notice

Corona SDK is not designed to be run in the background. We always expect the user interface to be available. Data updates that would result in a screen update simply can’t be backgrounded. Those updates every minute would need to go somewhere and your app would have to be in the foreground to receive them. It would take a major overhaul of the entire engine to support this, if was even possible.

Rob

Does this have anything to do with the “background app refresh” settings in iOS?
If corona apps cannot run in background, why do they appear enabled to do so in those settings panel?
Thanks
G.

Thanks Rob, the only way to run app in background is to use Corona SDK Enterprise or it isn’t possibile anyway because engine cannot do that

Wait a minute, so running apps in the background ( e.g handling location update / receiving data from internet ) is possible BUT only in Corona Enterprise and not in Corona SDK? Or it’s not possible with any version of Corona at all? Could someone clarify please? 

Thank you. 

You will have to know native code to make it run in the background …

I don’t now about Enterprise or CoronaCards. They might. The problem is the Lua side and the bridge in between. As long as your enterprise app doesn’t try to send data to the Lua side or expect information from the Lua side, then it ***MIGHT*** work.  You can get a trial of Enterprise and see what you can do.

**BUT** our official stance is we don’t support backgrounding and you may or may not get lucky, but we can’t provide any support or guarantee that it will work and if it does, we can’t guarantee it will work in the future.

Rob

I see, thanks a lot for clearing that up Rob. Well, my plan was to go Enterprise in the future and use my own Java APIs anyway. So I guess if I do have to process stuff in the background ( in the unlikely case for a business app) I might try and see what happens.

Someone has Corona Enterprise and can do a test? Run app in background and print coordinates in corona simulator… thanks