在后台运行应用程序 [英] Run the application in background
问题描述
我正在尝试使用kivy构建android应用.如何隐藏我的应用程序,但仍使其像恶魔般在后台运行?
I am trying to build android app using kivy. How I can hide my app but still keep it running in the background like a deamon?
from kivy.config import Config
Config.set('graphics', 'fullscreen', 'fake')
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
button = Button(text="Exit", size_hint=(None, None))
button.bind(on_press=exit)
return button
if __name__ == '__main__':
MyApp().run()
推荐答案
如果要在后台实际进行计算,则需要使用android服务.适用于Android的Python可以做到这一点,相关文档为此处.
You need to use an android service if you want to actually do computation in the background. Python-for-android can do this, the relevant documentation is here (old_toolchain). For the new toolchain method see here.
如果只希望您的应用程序不完全关闭(以免每次启动时都不会完全启动,则只需将on_pause
方法添加到您的App类中即可)应该返回True
.您也可以使用此方法执行任何暂停前的操作.但是,该应用程序并没有真正保持运行,只是保持了内存状态.
If you just want your app to not be closed completely (so that it doesn't restart entirely with the splash screen etc. every time), you just have to add an on_pause
method to your App class, and it should return True
. You can also do any pre-pause stuff in this method. However, the app doesn't really keep running, it just keeps memory state.
在后一种情况下,请注意,Android有时会杀死处于暂停状态的应用程序.这是处理应用程序的正常方式,您无法避免,因此您应该在on_pause
方法中保存任何重要状态.
In the latter case, be aware that android can and sometimes will kill apps in a pause state. This is a normal part of the way apps are handled and you can't avoid it, so you should save any important state in your on_pause
method.
这篇关于在后台运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!