【发布时间】:2017-10-15 21:23:22
【问题描述】:
我正在尝试在我的 kivy 应用程序中使用 android 通知,但遇到了问题。
起初我尝试使用 plyer,但它不起作用(应用程序只是崩溃)。因此,我阅读了一些文章并查看了一些示例,并尝试使用 jnius 自己完成并制作了这个简单的应用程序:
from kivy.app import App
from kivy.uix.button import Button
from jnius import autoclass
def notify(*args):
AndroidString = autoclass('java.lang.String')
PythonActivity = autoclass('org.kivy.android.PythonActivity')
NotificationBuilder = autoclass('android.app.Notification$Builder')
Drawable = autoclass('org.test.notify.R$drawable')
icon = Drawable.icon
notification_builder = NotificationBuilder(PythonActivity.mActivity)
notification_builder.setContentTitle(AndroidString('Title'.encode('utf-8')))
notification_builder.setContentText(AndroidString('Message'.encode('utf-8')))
notification_builder.setSmallIcon(icon)
notification_builder.setAutoCancel(True)
notification_service = PythonActivity.mActivity.getSystemService(PythonActivity.NOTIFICATION_SERVICE)
notification_service.notify(0,notification_builder.build())
class NotifyApp(App):
def build(self):
return Button(text="notify", on_press=notify)
if __name__ == '__main__':
NotifyApp().run()
但它也不起作用并且崩溃了。我不明白为什么,因为它与 this example 或 this example 相同,但我刚刚将 org.renpy.android 更改为 org.kivy.android 因为 renpy 已被弃用(但是 org.renpy.android 没有任何变化)。
据我了解,问题出在这条线上:
notification_service = PythonActivity.mActivity.getSystemService(PythonActivity.NOTIFICATION_SERVICE)
因为我在 notify() 函数中没有最后两行的情况下测试了这段代码并且它可以工作(但当然什么也不做)。
在 buildozer adb 日志中我可以看到这个错误:
10-15 16:57:00.432 22091 22112 F art : art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: static jfieldID 0x6fc46968 not valid for class java.lang.Class<org.kivy.android.PythonActivity>
我不擅长 Java,但在 plyer 和我发现的所有示例中都使用了相同的代码,而且它似乎适用于除我之外的所有人。
【问题讨论】:
-
请将“notification_service.notify(0,noti.build())”更改为“notification_service.notify(0, notification_builder.build())”。
-
@ikolim 谢谢,我已经更正了。但问题仍然存在。
标签: android python python-2.7 kivy