android - 如何防止应用程序在后台关闭?
问题描述
我使用 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 方法可以解决它。
推荐阅读
- php - 如何遍历json数据并发送到数据库中?
- python - python循环在行中插入值
- react-native - 如何使用 React native Ii18 进行语言更改?
- discord.js - 向所有服务器发送消息
- python - 如何使用 dfs 在图中找到桥?
- javascript - 如何在nodejs中阅读pdf
- visual-studio - 安装 Visual Studio 任何版本时的问题
- java - 在Java中为Activity添加标志
- java - Spring如何读取x-www-form-urlencoded content Type?
- maven - 在 npm 中进行本地测试的 maven install 相当于什么?