gpt4 book ai didi

android - 如何防止应用程序在后台关闭?

转载 作者:行者123 更新时间:2023-11-29 23:12:53 25 4
gpt4 key购买 nike

我使用 buildozer 在 kivy 上构建的每个 android 应用程序在进入后台后都会自行重启。即使我在应用程序上滑动通知时应用程序会重新启动。

我尝试查找有关此问题的文档,但没有找到任何内容。


from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
Camera:
id: camera
resolution: (640, 480)
play: False
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')


class CameraClick(BoxLayout):
def capture(self):
'''
Function to capture the images and give them the names
according to their captured time and date.
'''
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")


class TestCamera(App):

def build(self):
return CameraClick()


TestCamera().run()

这是我要运行的应用程序,默认的 buildozer.spec 只为相机添加 opencv。

最佳答案

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
Camera:
id: camera
resolution: (640, 480)
play: False
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')


class CameraClick(BoxLayout):
def capture(self):
'''
Function to capture the images and give them the names
according to their captured time and date.
'''
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")


class TestCamera(App):

def build(self):
return CameraClick()

def on_pause(self):
return True


TestCamera().run()

更改on_pause方法即可解决。

关于android - 如何防止应用程序在后台关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55786015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com