使用AccessibilityService读通知 [英] Reading notifications using AccessibilityService

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

问题描述

我最近尝试使用 AccessibilityService 来读取进入的通知。 我知道读通知使用 NotificationListenerService ,但是这不是我所需要(与较低的Andr​​oid版本的兼容性)。 我的问题是, OnServiceConnected()永远不会被调用,即使在我的设置,我已经给了我的应用程序留点premisions。 这是我的code:

I've recently tried to read incoming notifications using AccessibilityService. I know reading notifications using NotificationListenerService, but that's not what I need (for compatibility with lower android versions). My problem is, OnServiceConnected() never get called, even though in my settings, I've gave my app the neccessary premisions. Here is my code:

public class NotificationAccessibilityService extends AccessibilityService{

    protected void onServiceConnected() {
        Log.d("Tortuga", "AccessibilityService Connected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        setServiceInfo(info);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent e) {
        Log.d("Tortuga","FML");
        if (e.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            Log.d("Tortuga","Recieved event");
            Parcelable data = e.getParcelableData();
            if (data instanceof Notification) {
                Log.d("Tortuga","Recieved notification");
                Notification notification = (Notification) data;
                Log.d("Tortuga","ticker: " + notification.tickerText);
                Log.d("Tortuga","icon: " + notification.icon);
                Log.d("Tortuga", "notification: "+ e.getText());
            }
        }
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub
    }
}

和我的XML:

AndroidManifest.xml中

<service
            android:name="com.tortuga.shutapp.NotificationAccessibilityService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibilityservice" />
        </service>

XML \ accessibilityservice.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:notificationTimeout="100" />

请帮忙,我迷路了。我在Nexus 5当前正在运行。 谢谢!

Please help, I'm lost. I'm currently running on Nexus 5. Thanks!

推荐答案

你有没有启动的服务?设置 - 辅助功能 - 服务

Have you start the service? In "Setting - Accessibility - your service"

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

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