如何使用服务文件在后台运行我的 kivy 应用程序?
我正在构建一个 kivy 应用程序,我想在后台运行或在使用另一个应用程序时运行。
到目前为止,我确实有一个服务文件,但它似乎不起作用。该应用程序打包完美,没有任何错误,并且应用程序没有崩溃。 buildozer.spec 文件很好。那么任何人都可以告诉我我哪里出错了。
Main.py -
import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from plyer import accelerometer, spatialorientation, vibrator
from jnius import autoclass
spatialorientation.enable_listener()
accelerometer.enable()
time = 2
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.rows = 2
self.accel = Label(text = "unknown")
self.orien = Label(text = "unknown")
self.add_widget(self.accel)
self.add_widget(self.orien)
class MyApp(App):
def build(self):
self.service = autoclass('org.test.myapp.ServiceMyservice')
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
argument = ''
self.service.start(mActivity, argument)
self.grid = MyGrid()
Clock.schedule_interval(self.update, 2/10)
return self.grid
def update(self, dt):
accel1 = accelerometer.acceleration
accel2 = (round(accel1[0], 2), round(accel1[1], 2), round(accel1[2], 2))
new_accel = ', '.join(map(str, accel2))
orien1 = spatialorientation.orientation
orien2 = (round(orien1[0], 2), round(orien1[1], 2), round(orien1[2], 2))
new_orien = ', '.join(map(str, orien2))
if accel2[1] < 7 and orien2[1] > -0.65:
vibrator.vibrate(time = time)
else:
self.grid.accel.text = new_accel
self.grid.orien.text = new_orien
MyApp().run()
service.py -
from plyer import accelerometer, spatialorientation, vibrator, flash
from time import sleep
spatialorientation.enable_listener()
accelerometer.enable()
time = 2
def flashlight():
flash.on()
sleep(.2)
flash.off()
sleep(.2)
if __name__ == '__main__':
while True:
accel1 = accelerometer.acceleration
accel2 = (round(accel1[0], 2), round(accel1[1], 2), round(accel1[2], 2))
new_accel = ', '.join(map(str, accel2))
orien1 = spatialorientation.orientation
orien2 = (round(orien1[0], 2), round(orien1[1], 2), round(orien1[2], 2))
new_orien = ', '.join(map(str, orien2))
if accel2[1] < 7 and orien2[1] > -0.65:
vibrator.vibrate(time = time)
flashlight()
else:
sleep(.1)
flash.off()
日志脚本 -
03-06 12:53:08.677 23341 28506 I python : Android kivy bootstrap done. __name__ is __main__
03-06 12:53:08.677 23341 28506 I python : AND: Ran string
03-06 12:53:08.677 23341 28506 I python : Run user program, change dir and execute entrypoint
03-06 12:53:08.786 23341 28506 I python : [WARNING] [Config ] Older configuration version detected (0 instead of 21)
03-06 12:53:08.787 23341 28506 I python : [WARNING] [Config ] Upgrading configuration in progress.
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Logger ] Record log in /data/user/0/org.test.myapp/files/app/.kivy/logs/kivy_22-03-06_0.txt
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Kivy ] v2.0.0
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Kivy ] Installed at "/data/user/0/org.test.myapp/files/app/_python_bundle/site-packages/kivy/__init__.pyc"
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Python ] v3.8.9 (default, Feb 25 2022, 19:21:33)
03-06 12:53:08.792 23341 28506 I python : [Clang 8.0.2 (https://android.googlesource.com/toolchain/clang 40173bab62ec7462
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Python ] Interpreter at ""
03-06 12:53:08.793 23341 28506 I python : [INFO ] [Logger ] Purge log fired. Analysing...
03-06 12:53:08.793 23341 28506 I python : [INFO ] [Logger ] Purge finished!
03-06 12:53:09.684 23341 28506 I python : [INFO ] [Factory ] 186 symbols loaded
03-06 12:53:09.841 23341 28506 I python : [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2 (img_pil, img_ffpyplayer ignored)
03-06 12:53:09.862 23341 28506 I python : [INFO ] [Text ] Provider: sdl2
03-06 12:53:10.191 23341 28506 I python : [INFO ] [Window ] Provider: sdl2
03-06 12:53:10.381 28518 28539 I python : Initializing Python for Android
03-06 12:53:10.381 28518 28539 I python : Setting additional env vars from p4a_env_vars.txt
03-06 12:53:10.381 28518 28539 I python : Changing directory to the one provided by ANDROID_ARGUMENT
03-06 12:53:10.381 28518 28539 I python : /data/user/0/org.test.myapp/files/app
03-06 12:53:10.381 28518 28539 I python : Preparing to initialize python
03-06 12:53:10.381 28518 28539 I python : _python_bundle dir exists
03-06 12:53:10.381 28518 28539 I python : calculated paths to be...
03-06 12:53:10.381 28518 28539 I python : /data/user/0/org.test.myapp/files/app/_python_bundle/stdlib.zip:/data/user/0/org.test.myapp/files/app/_python_bundle/modules
03-06 12:53:10.382 28518 28539 I python : set wchar paths...
03-06 12:53:10.490 28518 28539 I python : Initialized python
03-06 12:53:10.490 28518 28539 I python : AND: Init threads
03-06 12:53:10.494 28518 28539 I python : AND: Ran string
03-06 12:53:10.494 28518 28539 I python : Run user program, change dir and execute entrypoint
03-06 12:53:10.719 23341 28506 I python : [INFO ] [GL ] Using the "OpenGL ES 2" graphics system
03-06 12:53:10.722 23341 28506 I python : [INFO ] [GL ] Backend used <sdl2>
03-06 12:53:10.723 23341 28506 I python : [INFO ] [GL ] OpenGL version <b'OpenGL ES 3.2 v1.r19p0-01rel0.###other-sha0123456789ABCDEF0###'>
03-06 12:53:10.724 23341 28506 I python : [INFO ] [GL ] OpenGL vendor <b'ARM'>
03-06 12:53:10.726 23341 28506 I python : [INFO ] [GL ] OpenGL renderer <b'Mali-G72'>
03-06 12:53:10.728 23341 28506 I python : [INFO ] [GL ] OpenGL parsed version: 3, 2
03-06 12:53:10.731 23341 28506 I python : [INFO ] [GL ] Texture max size <8192>
03-06 12:53:10.734 23341 28506 I python : [INFO ] [GL ] Texture max units <16>
03-06 12:53:10.800 23341 28506 I python : [INFO ] [Window ] auto add sdl2 input provider
03-06 12:53:10.803 23341 28506 I python : [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
03-06 12:53:10.995 23341 28506 I python : [WARNING] [Base ] Unknown <android> provider
03-06 12:53:10.995 23341 28506 I python : [INFO ] [Base ] Start application main loop
03-06 12:53:10.998 23341 28506 I python : [INFO ] [GL ] NPOT texture support is available
03-06 12:53:14.076 28518 28539 I python : Python for android ended.
03-06 12:53:19.769 23341 28506 I python : [INFO ] [Base ] Leaving application in progress...
03-06 12:53:19.814 23341 28506 I python : Python for android ended.
I am building a kivy app and i want to run in the background or when using another app.
So far I do have a service file, but it doesn't seem to working. The app packaged perfectly without any errors, and the app did not crash. The buildozer.spec file is good. So can anyone please tell me where I went wrong.
Main.py -
import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from plyer import accelerometer, spatialorientation, vibrator
from jnius import autoclass
spatialorientation.enable_listener()
accelerometer.enable()
time = 2
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.rows = 2
self.accel = Label(text = "unknown")
self.orien = Label(text = "unknown")
self.add_widget(self.accel)
self.add_widget(self.orien)
class MyApp(App):
def build(self):
self.service = autoclass('org.test.myapp.ServiceMyservice')
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
argument = ''
self.service.start(mActivity, argument)
self.grid = MyGrid()
Clock.schedule_interval(self.update, 2/10)
return self.grid
def update(self, dt):
accel1 = accelerometer.acceleration
accel2 = (round(accel1[0], 2), round(accel1[1], 2), round(accel1[2], 2))
new_accel = ', '.join(map(str, accel2))
orien1 = spatialorientation.orientation
orien2 = (round(orien1[0], 2), round(orien1[1], 2), round(orien1[2], 2))
new_orien = ', '.join(map(str, orien2))
if accel2[1] < 7 and orien2[1] > -0.65:
vibrator.vibrate(time = time)
else:
self.grid.accel.text = new_accel
self.grid.orien.text = new_orien
MyApp().run()
service.py -
from plyer import accelerometer, spatialorientation, vibrator, flash
from time import sleep
spatialorientation.enable_listener()
accelerometer.enable()
time = 2
def flashlight():
flash.on()
sleep(.2)
flash.off()
sleep(.2)
if __name__ == '__main__':
while True:
accel1 = accelerometer.acceleration
accel2 = (round(accel1[0], 2), round(accel1[1], 2), round(accel1[2], 2))
new_accel = ', '.join(map(str, accel2))
orien1 = spatialorientation.orientation
orien2 = (round(orien1[0], 2), round(orien1[1], 2), round(orien1[2], 2))
new_orien = ', '.join(map(str, orien2))
if accel2[1] < 7 and orien2[1] > -0.65:
vibrator.vibrate(time = time)
flashlight()
else:
sleep(.1)
flash.off()
log script -
03-06 12:53:08.677 23341 28506 I python : Android kivy bootstrap done. __name__ is __main__
03-06 12:53:08.677 23341 28506 I python : AND: Ran string
03-06 12:53:08.677 23341 28506 I python : Run user program, change dir and execute entrypoint
03-06 12:53:08.786 23341 28506 I python : [WARNING] [Config ] Older configuration version detected (0 instead of 21)
03-06 12:53:08.787 23341 28506 I python : [WARNING] [Config ] Upgrading configuration in progress.
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Logger ] Record log in /data/user/0/org.test.myapp/files/app/.kivy/logs/kivy_22-03-06_0.txt
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Kivy ] v2.0.0
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Kivy ] Installed at "/data/user/0/org.test.myapp/files/app/_python_bundle/site-packages/kivy/__init__.pyc"
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Python ] v3.8.9 (default, Feb 25 2022, 19:21:33)
03-06 12:53:08.792 23341 28506 I python : [Clang 8.0.2 (https://android.googlesource.com/toolchain/clang 40173bab62ec7462
03-06 12:53:08.792 23341 28506 I python : [INFO ] [Python ] Interpreter at ""
03-06 12:53:08.793 23341 28506 I python : [INFO ] [Logger ] Purge log fired. Analysing...
03-06 12:53:08.793 23341 28506 I python : [INFO ] [Logger ] Purge finished!
03-06 12:53:09.684 23341 28506 I python : [INFO ] [Factory ] 186 symbols loaded
03-06 12:53:09.841 23341 28506 I python : [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2 (img_pil, img_ffpyplayer ignored)
03-06 12:53:09.862 23341 28506 I python : [INFO ] [Text ] Provider: sdl2
03-06 12:53:10.191 23341 28506 I python : [INFO ] [Window ] Provider: sdl2
03-06 12:53:10.381 28518 28539 I python : Initializing Python for Android
03-06 12:53:10.381 28518 28539 I python : Setting additional env vars from p4a_env_vars.txt
03-06 12:53:10.381 28518 28539 I python : Changing directory to the one provided by ANDROID_ARGUMENT
03-06 12:53:10.381 28518 28539 I python : /data/user/0/org.test.myapp/files/app
03-06 12:53:10.381 28518 28539 I python : Preparing to initialize python
03-06 12:53:10.381 28518 28539 I python : _python_bundle dir exists
03-06 12:53:10.381 28518 28539 I python : calculated paths to be...
03-06 12:53:10.381 28518 28539 I python : /data/user/0/org.test.myapp/files/app/_python_bundle/stdlib.zip:/data/user/0/org.test.myapp/files/app/_python_bundle/modules
03-06 12:53:10.382 28518 28539 I python : set wchar paths...
03-06 12:53:10.490 28518 28539 I python : Initialized python
03-06 12:53:10.490 28518 28539 I python : AND: Init threads
03-06 12:53:10.494 28518 28539 I python : AND: Ran string
03-06 12:53:10.494 28518 28539 I python : Run user program, change dir and execute entrypoint
03-06 12:53:10.719 23341 28506 I python : [INFO ] [GL ] Using the "OpenGL ES 2" graphics system
03-06 12:53:10.722 23341 28506 I python : [INFO ] [GL ] Backend used <sdl2>
03-06 12:53:10.723 23341 28506 I python : [INFO ] [GL ] OpenGL version <b'OpenGL ES 3.2 v1.r19p0-01rel0.###other-sha0123456789ABCDEF0###'>
03-06 12:53:10.724 23341 28506 I python : [INFO ] [GL ] OpenGL vendor <b'ARM'>
03-06 12:53:10.726 23341 28506 I python : [INFO ] [GL ] OpenGL renderer <b'Mali-G72'>
03-06 12:53:10.728 23341 28506 I python : [INFO ] [GL ] OpenGL parsed version: 3, 2
03-06 12:53:10.731 23341 28506 I python : [INFO ] [GL ] Texture max size <8192>
03-06 12:53:10.734 23341 28506 I python : [INFO ] [GL ] Texture max units <16>
03-06 12:53:10.800 23341 28506 I python : [INFO ] [Window ] auto add sdl2 input provider
03-06 12:53:10.803 23341 28506 I python : [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
03-06 12:53:10.995 23341 28506 I python : [WARNING] [Base ] Unknown <android> provider
03-06 12:53:10.995 23341 28506 I python : [INFO ] [Base ] Start application main loop
03-06 12:53:10.998 23341 28506 I python : [INFO ] [GL ] NPOT texture support is available
03-06 12:53:14.076 28518 28539 I python : Python for android ended.
03-06 12:53:19.769 23341 28506 I python : [INFO ] [Base ] Leaving application in progress...
03-06 12:53:19.814 23341 28506 I python : Python for android ended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 main.py 中,定义服务并启动它:
在 buildozer.spec 中:
in main.py, define the service and start it:
in buildozer.spec: