【发布时间】:2015-08-12 16:48:59
【问题描述】:
我最近发现了 Kivy,并且一直在玩一些简单的例子。我修改了 Dusty Phillips 书中在 Kivy 中创建应用程序中的一些代码,但无法在我的 Android 手机上获得 GPS 输出。我只是想让代码每秒显示一次位置。这是我的 main.py 文件的相关部分:
class AddLocationForm(BoxLayout):
def myclock(self, *args):
try:
gps.start()
except:
self.outputs.item_strings=["", "No Location", str(random.random())]
@mainthread
def on_location(self, **kwargs):
try:
self.outputs.item_strings = ["","","", str(kwargs['lat']),"","","",str(kwargs['lon']), "","","",str(random.random())]
except:
pass
def on_status(sefl, **kwargs):
self.outputs.item_strings = ["", "","", "On status", "", "",str(random.random())]
def send_location(self):
try:
gps.configure(on_location=self.on_location, on_status=self.on_status)
Clock.schedule_interval(self.myclock, 1)
except NotImplementedError:
self.outputs.item_strings=["", "No GPS", str(random.random())]
class WeatherApp(App):
def on_pause(self):
return True
if __name__=='__main__':
WeatherApp().run()
我的 KV 文件是:
AddLocationForm:
<AddLocationForm>:
orientation: "vertical"
outputs: outputs_list
BoxLayout:
height: "40dp"
size_hint_y: None
Button:
text: "Start"
size_hint_x: 25
on_press: root.send_location()
Button:
text: "End"
size_hint_x: 25
ListView:
id: outputs_list
item_strings: []
该应用程序在我的桌面上运行良好,因为它会遇到 NotImplementedError。当我编译应用程序(使用 buildozer 通过运行buildozer android debug 创建 .apk 文件)并将其安装在我的 Android 手机上时,它似乎一开始可以工作大约一分钟,然后冻结而没有任何响应。起初在后台暂停/运行似乎不是问题。例如,我可以在应用程序之间切换并返回它,它仍然会输出位置。然而,一段时间后,应用程序只是冻结了......没有多次运行send_location()
我已在 buildozer.specs 文件中启用访问权限和权限以及 plyer 库
【问题讨论】:
-
logcat 是否提供任何有用的信息?另外,您是否添加了 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限?
-
瑞恩,感谢您的建议。我同时使用 Fine 和 Coarse 位置。我实际上是通过将 kwargs['lat'] 修改为 str(kwargs['lat']) 来使其工作的,但是,现在该应用程序似乎在一分钟左右后变得无响应(尽管它起初可以工作)有什么建议吗?跨度>
-
您上面的代码中没有足够的信息来回答这个问题。不过,如果您不止一次致电
send_location,这可能就是问题所在。 -
再次感谢您的回复。我已经修改了上面的代码以包含所有内容,包括 KV 文件 ....