检测一个新的Andr​​oid的通知 [英] Detect a new Android notification

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

问题描述

在Android的应用程序,我的工作,我想,当一个新的状态栏通知出现,不管它是否是由我的应用程序引起的,就能够检测到。更具体地,我要计数在给定时间帧通知的数量。

In the Android app that I'm working on, I'd like to be able to detect when a new status bar notification appears, regardless of if it was caused by my app. To be more specific, I want to count the number of notifications in a given time frame.

这甚至有可能,如果是这样,怎么样?

Is this even possible, and if so, how?

推荐答案

其实,这是可能的,我在我的应用程序中使用它。

Actually, it is possible, I use it in my app.

您需要注册一个<一个href="http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html">AccessibilityService并确保用户启用该服务。

You need to register an AccessibilityService and make sure the user enables the service.

实例服务:

public class InstantMessenger extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        //Do something, eg getting packagename
        final String packagename = String.valueOf(event.getPackageName());  
}
}

@Override
protected void onServiceConnected() {
    if (isInit) {
        return;
    }
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
    setServiceInfo(info);
    isInit = true;
}

@Override
public void onInterrupt() {
    isInit = false;
}
}

<一个href="http://stackoverflow.com/questions/5081145/android-how-do-you-check-if-a-particular-accessibilityservice-is-enabled">Example检查如果您的服务已激活

这篇关于检测一个新的Andr​​oid的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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