首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Kivy/Pyjnius android NotificationListenerService

Python Kivy/Pyjnius android NotificationListenerService
EN

Stack Overflow用户
提问于 2021-03-16 17:53:36
回答 1查看 282关注 0票数 0

我想用python中的kivy为android创建一个应用程序,它可以侦听通知。

我创建了一个notification_listener.py:

代码语言:javascript
复制
from kivy import platform


if platform == "android":
    from jnius import autoclass, cast, PythonJavaClass, java_method
    from android.runnable import run_on_ui_thread


    PythonActivity = autoclass("org.kivy.android.PythonActivity")

    Activity = autoclass("android.app.Activity")
    Context = autoclass("android.content.Context")
    NotificationListenerService = autoclass("android.service.notification.NotificationListenerService")
    StatusBarNotification = autoclass("android.service.notification.StatusBarNotification")
    Log = autoclass("android.util.Log")
    Toast = autoclass("android.widget.Toast")

    String = autoclass("java.lang.string")
    CharSequence = autoclass("java.lang.CharSequence")

    activity = PythonActivity.mActivity
    currentActivity = cast(Activity, activity)
    context = cast(Context, currentActivity.getApplicationContext())


    class NotificationListener(PythonJavaClass):
        __javaclass__ = "android.service.notification.NotificationListenerService"
        __javacontext__ = "app"

        @java_method("()V")
        def onCreate(self):
            super(NotificationListener, self).onCreate()

            text = cast(CharSequence, String("Listener started..."))
            toast = Toast.makeText(context, text, Toast.LENGTH_LONG)
            toast.show()

        @java_method("(Landroid/service/notification/StatusBarNotification)V")
        def onNotificationPosted(self, sbn):
            notification = cast(StatusBarNotification, sbn)
            extras = notification.getNotification().extras

            tag = String("Notification recived")
            msg_title = String("title: %s" % (extras.getString("android.title")))
            msg_text = String("text: %s" % (extras.getString("android.text")))

            Log.v(tag, msg_title)
            Log.v(tag, msg_text)

            text = cast(CharSequence, String("Notification recieved..."))
            toast = Toast.makeText(context, text, Toast.LENGTH_LONG)
            toast.show()

但是,我必须如何将添加到AndroidManifest.xml?如果我使用Java语言,下面的代码是正确的,但它是一个python文件,那么我必须如何实现它呢??

代码语言:javascript
复制
<service name=".NotificationListener"
    android:label="notification_listener"
    android:permissions="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>
EN

回答 1

Stack Overflow用户

发布于 2021-08-21 15:36:33

最近我让它工作了,所以我在这里写我的经验。

Buildozer不提供任何用于注册具有权限和意图过滤器的服务的选项。但是,您可以从位于.buildozer目录中的模板文件自定义AndroidManifest.xml。

代码语言:javascript
复制
/your/project/home/.buildozer/android/platform/build-armeabi-v7a/dists/yourapp__armeabi-v7a/templates/AndroidManifest.tmpl.xml

您可以在"end-if“和"if”之间插入XML块,这样它就不会被buildozer.spec设置更改。

你需要做的下一件事是创建一个java类文件。不幸的是,python类不能绑定到NotificationListeners,因此您需要手动复制一个接收StatusBarNotifications的java类。

路径将是:

代码语言:javascript
复制
/your/project/home/.buildozer/android/platform/build-armeabi-v7a/dists/yourapp__armeabi-v7a/src/main/java/your/app/domain/NotificationListener.java

使用java类,您可以将StatusBarNotification广播到Python端,getApplicationContext().sendBroadcast(intent);,P4A的android.broadcast.BroadcastReceiver将在main.py上接收该对象。

Java类

代码语言:javascript
复制
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    try {
        Bundle bundle = new Bundle();
        bundle.putParcelable("sbn",sbn);
        Intent intent = new Intent("action_name_matches_with_java_side");
        intent.putExtra("sbn",sbn);
        getApplicationContext().sendBroadcast(intent);
    } catch(Exception e) {
        Log.v("KIVYAPP","Notification broadcasting prohibited");
    }
}

main.py

代码语言:javascript
复制
class YourApp(App):
    def build(self):
        ...
        self.br = BroadcastReceiver(self.onReceive,actions=["action_name_matches_with_java_side"])
        self.br.start()
        ...

    def onReceive(self, context, intent):
        bundle = cast(Bundle, intent.getExtras())
        sbn = cast(StatusBarNotification,bundle.getParcelable("sbn"))
        ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66652794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档