Kivy和Android通知 [英] Kivy and android notifications

查看:388
本文介绍了Kivy和Android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的kivy应用程序中使用android通知,但遇到了问题.

I'm trying to use android notifications in my kivy application and I'm having a problem.

起初,我尝试使用plyer,但它不起作用(应用程序崩溃).所以我读了一些文章,看了一些例子,并尝试使用jnius自己做,并制作了这个简单的应用程序:

At first I've tried to use plyer but it doesn't work (app just crashes). So I've read some articles and looked some examples and tried to do it myself using jnius and made this simple application:

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()

但是它不起作用并且也会崩溃.我不明白为什么,因为它与此示例此示例,但我只是将org.renpy.android更改为org.kivy. android,因为renpy已过时(但是org.renpy.android没有任何变化).

But it doesn't work and crashes too. I can't understand why because it's the same as in this example or this example but I've just changed org.renpy.android to org.kivy.android because renpy is deprecated (however with org.renpy.android nothing changes).

据我所知,问题所在:

notification_service = PythonActivity.mActivity.getSystemService(PythonActivity.NOTIFICATION_SERVICE)

因为我已经在notify()函数的最后两行中测试了此代码,所以它可以工作(但当然不起作用).

Because I've tested this code without 2 last lines in notify() function and it works (but of course does nothing).

在buildozer adb日志中,我可以看到此错误:

In buildozer adb log I can see this error:

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和我发现的所有示例中都使用了相同的代码,似乎它对除我以外的所有人都适用.

I'm not good in Java but the same code is used in plyer and in all examples that I've found and it seems that it works for everyone except me.

推荐答案

我已经找到了解决方案

I have found the solution here. I just had to use Context. So work code looks like this:

Context = autoclass('android.content.Context')
notification_service = PythonActivity.mActivity.getSystemService(Context.NOTIFICATION_SERVICE)

我希望这对遇到相同问题的人有所帮助.

I hope this helps those who encounter the same problem.

这篇关于Kivy和Android通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆