如何在我的应用中实现日常本地通知?所以在第一次运行时我需要在14:00注册第一个本地通知,然后每天14:00注册通知。
用户可以在应用设置中更改此时间。
如何做这个机制?
答案 0 :(得分:1)
您需要处理结束时的通知,但要制作一个每天倒计时一定时间的计时器,您需要执行以下操作:
local targetDate = os.time{ year=2014, month=11, day=8, hour=0, sec=0 } -- Get the date that you want to count down to, in seconds
local text = false
local function enterFrame(event)
if text then text:removeSelf() end -- Everyframe, remove the old text object
local timeRemaining = (targetDate-os.time()) -- Take the difference between the target time and the current time
local days = timeRemaining / 86400 -- get the number of days left by dividing the remaining seconds by the number of seconds in a day
local hours = days%1 * 24 -- get the number of hours left by multiplying the remainder by the number hours in a day
local minutes = hours%1 * 60 -- get the number of minutes by multiplying the remainder by the number of minutes in an hour
local seconds = math.floor( minutes%1 * 60 + 0.5) -- multiply the remainder one more time by the number of seconds in a minute, and round to the nearest second.
-- make a new text object to display all the info
text = display.newText( "Will be available in "..math.floor(days).." days "..math.floor(hours).." hrs "..math.floor(minutes).." mins "..seconds.." secs ", 25, 140)
end
Runtime:addEventListener( "enterFrame", enterFrame )
您可以重新使用代码来处理项目,但是您需要使用操作系统时间并对每个变量进行某些计算以获得时间。祝你好运。