Is there a way to keep timer running when app switched or in standby?

I have a timer app that pauses when the user hits the standby (screen off) button on the iphone. The timer in my app pauses until the the screen is back on. Is there a way to keep the timer going regardless of whether the user does something else (screen saver, answers call, runs another app, etc) [import]uid: 101325 topic_id: 29573 reply_id: 329573[/import]

Unfortunately not.

The LUA engine gets shutdown when the app goes into the background, hence no event listeners, timers, or anything else related to LUA is available. All LUA processes get suspended.

Certain “background” processes that were launched prior to LUA being shut down, (and if the UIApplicationExitsOnSuspend and one of the UIBackgroundModes are set) may continue to operate, such as a download, or a background audio that was launched prior to LUA suspension.

However, these processes will not be able to “call back” to your app, since LUA is suspended, so they will be sorta-kina free-floating until they complete.

LUA is kind of like an intermediary processor, which shuffles off commands to the underlying system/C++/Objective C. So LUA sits on top of the Corona “core”.

Apple only allows minimal and processes to run in the background – and only for a short period of time.

Corona’s LUA engine ain’t no “minimal” process, yo.

[import]uid: 616 topic_id: 29573 reply_id: 118723[/import]

I imagine you could come up with a solution using the system time. [import]uid: 147305 topic_id: 29573 reply_id: 118735[/import]

Thanks for the answer. I was afraid of that. Somebody showed my the iphone timer that continues even when screen suspended. I figured since that was a apple app, they may have had a special way to keep the timer going. [import]uid: 101325 topic_id: 29573 reply_id: 118757[/import]

I wrote a basic template. I think it should work this way.
Of course, you need to format the tables properly like date and time tables to be able to perform the mathematical functions like time_now - time_end .

[code]
local loadsave = require (“loadsave”) – json module by robmiracle

local time
local startTime

function start_timer()

startTime = system.getTimer() – runs in milliseconds
time = os.date( “*t” ) – returns table of year, date and time
end

function pause_timer()
– save time
mySettings.time_end = time – save the time, when timer was first started/ resumed
mySettings.startTime = startTime – save timer, eg. 03:12 3 mins and 12 seconds
loadsave.saveTable(mySettings, “mySettings.json”)
end

function resume_timer()
– load previous time
mySettings = loadsave.loadTable(“mySettings.json”)
local time_end = mySettings.time_end
startTime = mySettings.startTime

local time_now = os.date( “*t” )

time_difference = time_now - time_end – eg: 14:00 pm - 13:00 pm = 01:00 hour

time = time_difference + startTime – eg: 01:00 hour + 00:03:12 = 01:03:12

end

local function onSystemEvent( event )
if (event.type == “applicationSuspend”) or (event.type == “applicationExit”) then
pause_timer()
elseif (event.type == “applicationOpen”) or (event.type = “applicationStart”) then
resume_timer()
end
end
Runtime:addEventListener( “system”, onSystemEvent )
[/code] [import]uid: 98393 topic_id: 29573 reply_id: 118829[/import]

Thanks I’ll check that out.

That code should work for ios and android, right? [import]uid: 101325 topic_id: 29573 reply_id: 119784[/import]

Thanks I’ll check that out.

That code should work for ios and android, right? [import]uid: 101325 topic_id: 29573 reply_id: 119784[/import]

I rewrote the whole code! I don’t have an android phone. Please test it and tell me if it works! It works on simulator and iPad for me! You can suspend the app and do other stuff, go back to app and timer is still going. You can even shut down your device and the timer is still going.
you need loadsave.lua from the community code. Otherwiese just copy/paste it.

EDIT: arch my code is still off, it works only or the first time, you’ve to save the actual time (as prev_time) as well and add it like so :
time_txt.text = prev_time+secs+time_difference…“s”

save prev_time like
prev_time = prev_time+secs+time_difference
go figure

local loadsave = require ("loadsave") -- json module by robmiracle  
local widget = require "widget"  
  
local secs, reset\_btn  
  
-- json table  
local mySettings = {}  
  
local path\_check = system.pathForFile( "mySettings.json", system.DocumentsDirectory)  
local file = io.open( path\_check, "r" )  
  
-- app was opened the first time  
if file == nil then  
 mySettings.secs = 0  
 mySettings.time\_end = 0  
 loadsave.saveTable(mySettings, "mySettings.json")  
end  
  
   
function pause\_timer()  
 -- save time   
  
 timer.pause( Timer )  
 mySettings.secs = secs -- save the secs, when timer was first started/ resumed  
 mySettings.time\_end = os.time()  
 loadsave.saveTable(mySettings, "mySettings.json")  
end  
  
local time\_txt = display.newText("", 120, 60, native.systemFont, 10)  
time\_txt:setTextColor(255, 0, 0)   
  
function resume\_timer()   
 -- load previous time  
 mySettings = loadsave.loadTable("mySettings.json")  
  
 local time\_end = mySettings.time\_end   
 local time\_now = os.time()  
 secs = mySettings.secs   
  
  
 if time\_end ~= 0 then  
 time\_difference = os.difftime (time\_now, time\_end)   
 else   
 time\_difference = 0  
 end  
 time\_txt.text = "time\_difference since application closed/suspended: "..time\_difference  
  
 timer.resume( Timer )   
  
end  
local time\_txt = display.newText("", 120, 20, native.systemFont, 30)  
time\_txt:setTextColor(255, 0, 0)  
  
local function updateTime(event)  
 secs = secs+1  
 time\_txt.text = secs+time\_difference.."s"  
end  
  
Timer = timer.performWithDelay(1000, updateTime, 0)  
  
local onButtonEvent = function (event )  
 if event.phase == "release" then  
 secs = 0  
 time\_difference = 0  
 mySettings.secs = 0  
 mySettings.time\_end = 0   
 loadsave.saveTable("mySettings.json")   
 end  
end  
   
local reset\_btn = widget.newButton{  
 id = "reset",  
 left = 100,  
 top = 200,  
 label = "Reset Json Time",  
 width = 150, height = 28,  
 cornerRadius = 8,  
 onEvent = onButtonEvent  
}  
   
local function onSystemEvent( event )  
 if (event.type == "applicationExit") or (event.type == "applicationSuspend") then  
 pause\_timer()  
 elseif (event.type == "applicationStart") or (event.type == "applicationResume") then  
 resume\_timer()  
 end  
end  
  
Runtime:addEventListener( "system", onSystemEvent )  

[import]uid: 98393 topic_id: 29573 reply_id: 121266[/import]

I rewrote the whole code! I don’t have an android phone. Please test it and tell me if it works! It works on simulator and iPad for me! You can suspend the app and do other stuff, go back to app and timer is still going. You can even shut down your device and the timer is still going.
you need loadsave.lua from the community code. Otherwiese just copy/paste it.

EDIT: arch my code is still off, it works only or the first time, you’ve to save the actual time (as prev_time) as well and add it like so :
time_txt.text = prev_time+secs+time_difference…“s”

save prev_time like
prev_time = prev_time+secs+time_difference
go figure

local loadsave = require ("loadsave") -- json module by robmiracle  
local widget = require "widget"  
  
local secs, reset\_btn  
  
-- json table  
local mySettings = {}  
  
local path\_check = system.pathForFile( "mySettings.json", system.DocumentsDirectory)  
local file = io.open( path\_check, "r" )  
  
-- app was opened the first time  
if file == nil then  
 mySettings.secs = 0  
 mySettings.time\_end = 0  
 loadsave.saveTable(mySettings, "mySettings.json")  
end  
  
   
function pause\_timer()  
 -- save time   
  
 timer.pause( Timer )  
 mySettings.secs = secs -- save the secs, when timer was first started/ resumed  
 mySettings.time\_end = os.time()  
 loadsave.saveTable(mySettings, "mySettings.json")  
end  
  
local time\_txt = display.newText("", 120, 60, native.systemFont, 10)  
time\_txt:setTextColor(255, 0, 0)   
  
function resume\_timer()   
 -- load previous time  
 mySettings = loadsave.loadTable("mySettings.json")  
  
 local time\_end = mySettings.time\_end   
 local time\_now = os.time()  
 secs = mySettings.secs   
  
  
 if time\_end ~= 0 then  
 time\_difference = os.difftime (time\_now, time\_end)   
 else   
 time\_difference = 0  
 end  
 time\_txt.text = "time\_difference since application closed/suspended: "..time\_difference  
  
 timer.resume( Timer )   
  
end  
local time\_txt = display.newText("", 120, 20, native.systemFont, 30)  
time\_txt:setTextColor(255, 0, 0)  
  
local function updateTime(event)  
 secs = secs+1  
 time\_txt.text = secs+time\_difference.."s"  
end  
  
Timer = timer.performWithDelay(1000, updateTime, 0)  
  
local onButtonEvent = function (event )  
 if event.phase == "release" then  
 secs = 0  
 time\_difference = 0  
 mySettings.secs = 0  
 mySettings.time\_end = 0   
 loadsave.saveTable("mySettings.json")   
 end  
end  
   
local reset\_btn = widget.newButton{  
 id = "reset",  
 left = 100,  
 top = 200,  
 label = "Reset Json Time",  
 width = 150, height = 28,  
 cornerRadius = 8,  
 onEvent = onButtonEvent  
}  
   
local function onSystemEvent( event )  
 if (event.type == "applicationExit") or (event.type == "applicationSuspend") then  
 pause\_timer()  
 elseif (event.type == "applicationStart") or (event.type == "applicationResume") then  
 resume\_timer()  
 end  
end  
  
Runtime:addEventListener( "system", onSystemEvent )  

[import]uid: 98393 topic_id: 29573 reply_id: 121266[/import]

I get this error when I run your code:

Runtime error
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: attempt to index local ‘loadsave’ (a boolean value)
stack traceback:
[C]: ?
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: in main chunk
Runtime error: …54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: attempt to index local ‘loadsave’ (a boolean value)
stack traceback:
[C]: ?
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: in main chunk [import]uid: 101325 topic_id: 29573 reply_id: 123027[/import]

I get this error when I run your code:

Runtime error
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: attempt to index local ‘loadsave’ (a boolean value)
stack traceback:
[C]: ?
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: in main chunk
Runtime error: …54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: attempt to index local ‘loadsave’ (a boolean value)
stack traceback:
[C]: ?
…54ll01t6g3z72xpjh0000gn/T/TemporaryItems/94/main.lua:17: in main chunk [import]uid: 101325 topic_id: 29573 reply_id: 123027[/import]