我再次尝试https://github.com/kivy/plyer/blob/master/examples/gps/main.py的kivy gps示例。但是当我按下“开始”按钮并将手机置于睡眠模式(屏幕关闭)时出现问题,有时我回到屏幕和应用程序,只有我可以看到由于无法更新标签时冻结的gui待机模式。我怎么能克服这个?即使我的手机处于睡眠模式,我也希望更新标签。
from kivy.lang import Builder
from plyer import gps
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import Clock
kv = '''
BoxLayout:
orientation: 'vertical'
Label:
text: app.gps_location
Label:
text: app.gps_status
BoxLayout:
size_hint_y: None
height: '48dp'
padding: '4dp'
ToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop'
on_state: app.gps.start() if self.state == 'down' else app.gps.stop()
'''
def mainthread(func):
# This method is now part of Kivy 1.8.0. When it's released, remove it.
def delayed_func(*args, **kwargs):
def callback_func(dt):
func(*args, **kwargs)
Clock.schedule_once(callback_func, 0)
return delayed_func
class GpsTest(App):
def on_pause(self):
# Here you can save data if needed
return True
def on_resume(self):
# Here you can check if any data needs replacing (usually nothing)
pass
gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates')
def build(self):
self.gps = gps
try:
self.gps.configure(on_location=self.on_location,
on_status=self.on_status)
except NotImplementedError:
import traceback; traceback.print_exc()
self.gps_status = 'GPS is not implemented for your platform'
return Builder.load_string(kv)
@mainthread
def on_location(self, **kwargs):
self.gps_location = '\n'.join([
'{}={}'.format(k, v) for k, v in kwargs.items()])
@mainthread
def on_status(self, stype, status):
self.gps_status = 'type={}\n{}'.format(stype, status)
if __name__ == '__main__':
GpsTest().run()
答案 0 :(得分:0)
您无法保证应用会继续在睡眠模式下运行,因此这可能不是一个好方法。更好的是使用一个Android服务,这可能与python-for-android。 Here是关于它的博文,它是documented here。
您可以在服务中执行gps内容并与on_resume和/或on_start上的应用同步。