【问题标题】:Corona SDK: Restart an app only when user press "home"Corona SDK:仅在用户按“主页”时重新启动应用程序
【发布时间】:2013-04-04 17:01:28
【问题描述】:

使用 corona SDK,我希望每次用户点击主页按钮时都重新启动我的应用程序。 如果他/她接到电话,拉下下拉菜单等,我希望应用程序继续保持其当前状态。

有什么建议吗?

谢谢, /S

【问题讨论】:

  • 我猜你的意思是 iPhone ?
  • android(带有主页按钮的..)和 iPhone。谢谢

标签: lua coronasdk onpause


【解决方案1】:

我是怎么解决的!

suspendTime = 0
resumeTime = 0

function onSystemEvent( event )
    if event.type == "applicationSuspend" then
        suspendTime = os.time()
        print(suspendTime)
    elseif event.type == "applicationResume" then
        resumeTime = os.time()
        print(resumeTime)
        print("deltaTime: "..resumeTime - suspendTime )
            if(resumeTime - suspendTime > 30) then
            local sceneName = storyboard.getCurrentSceneName()
            if(sceneName ~= "levels.splash") then
                print(sceneName)
                print(resumeTime)
                        storyboard.gotoScene("levels.splash")
            end
        end
    end

end
Runtime:addEventListener("system", onSystemEvent)

【讨论】:

  • 不要忘记suspendTime = 0和resumeTime = 0,找到增量后;)顺便说一句,os.time()是否总是返回小时?当您在 23:59 暂停应用程序并在 00:01 再次打开该应用程序时会发生什么?
  • 我相信 os:time 是 unix 时间戳。 en.wikipedia.org/wiki/Unix_time
  • 所以,我猜它不会重新运行分钟。无论如何,您可以通过尝试或操纵该值来确保。如果它始终运行良好,则无需做任何事情..
【解决方案2】:
function onKeyEvent( event )
    local keyname = event.keyName;
    if (event.phase == "up" and (event.keyName=="back" or event.keyName=="menu" or event.keyName == "home" )) then
        if keyname == "menu" then
        os.exit()
    end
    end
    return false
end

Runtime:addEventListener( "key", onKeyEvent )

这个适用于安卓系统。 我检查了http://docs.coronalabs.com/api/event/key/keyName.html,所以在 iPhone 中没有办法做到这一点。

但是你可以试试这个:获取应用程序暂停的时间。并将其保存到文档目录。然后当应用程序恢复时,检查两个会话之间的时间。如果超过半小时,重新启动所有的东西。

【讨论】:

  • 不,这不适用于 android 上的主页按钮,正如您在链接中看到的那样,它说:homePageThe "navigate to home page" key (this is not the "Home" button on Android). 眼球答案就像魅力一样!
猜你喜欢
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2017-09-24
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多