Timers Help

Hello,

I posted yesterday but no one replied :frowning:

I was just wondering if it is possible to have a timer running (like a stopwatch timer) while the screen is blank, effectively so i can save battery life.

I have tried Beebes and Peaches timer but i am having no luck. It must be possible because other apps have them.

Does anyone have any idea? [import]uid: 67534 topic_id: 22746 reply_id: 322746[/import]

You may have to elaborate a bit more. Does the timer time how long the screen is blank? You mention other apps have them. Which ones? Might give a clearer idea of what you need. [import]uid: 31262 topic_id: 22746 reply_id: 90786[/import]

Umm I’m making a kind of stopwatch app which plus 1 every second until it reached 59 then it adds one to te minutes (using the timer.performwithDelay). However when I test the app using a motorola defy, the timer stops increasing when the screen goes off, even for like 10 seconds. I can stop the screen from turning off put then it wastes battery life.

Any idea on how to keep the timer going while the screen is off?

Btw I’m using android.

Oh yeah the other apps I saw that worked were google tracks and a bike timer.

Thankyou [import]uid: 67534 topic_id: 22746 reply_id: 90803[/import]

If the app is suspended/sleeping you may want to mark the time then when resumed calculate the difference.

Peach :slight_smile: [import]uid: 52491 topic_id: 22746 reply_id: 90892[/import]

Ah ok thank very much Peach! I didn’t realise I could do that, I shall go give it a go.

Cheers [import]uid: 67534 topic_id: 22746 reply_id: 90919[/import]

*edit* I think i have the right thing now but it still doesnt do what i want it to…

The thing is when the app is suspended, the system.getTimer() stays the same, therefore it wont add the correct amount of time to it.

Any ideas, this is what i have:

[code]

local bg = display.newRect( 0, 0, 320, 480 )
local minsText = 0
local secsText = 00
local timeText = display.newText(minsText… “:0” …secsText , 40, 40, “Helvetica”, 24)
timeText:setTextColor(0,0,0)

local Btn = display.newRect( 70, 70, 32, 48 )
Btn:setFillColor (0, 0, 0)

local function updateTime (event)
secsText = secsText + 1

if secsText < 10 then secsText = “0” …secsText elseif secsText > 59 then
secsText = 00
minsText = minsText+1
end
timeText.text = minsText … “:” …secsText
end
Timer = timer.performWithDelay(1000, updateTime, 0)
local function suspend (e)
if( e.type == “applicationSuspend” ) then
print(“applicationSuspend”)

t1 = system.getTimer()
print(“t1 =” … t1)
end
end
function resume (e)
if ( e.type == “applicationResume” ) then
print(“applicationResume”)

local function runningTime(event)
t2 = system.getTimer()
ms = t2 - t1
print("elapsed milliseconds = " … ms)
print (“t2 =” … t2)

local floor = math.floor

secsText = floor(secsText + (ms / 1000))
minsText= floor(minsText + (secsText / 60))

timeText.text = minsText … “:” … secsText

end
timer1 = timer.performWithDelay(1, runningTime, 1)
end
end

Runtime:addEventListener (“system”, suspend)
Runtime:addEventListener (“system”, resume) [import]uid: 67534 topic_id: 22746 reply_id: 90936[/import]

Don’t use system.getTimer - get the actual time of the device using os.time() (see API section) and then calculate difference and add that on :slight_smile: [import]uid: 52491 topic_id: 22746 reply_id: 91048[/import]

Ah thank you very much again Peach :slight_smile: [import]uid: 67534 topic_id: 22746 reply_id: 91088[/import]