检测新的 Android 通知 [英] Detect a new Android notification
本文介绍了检测新的 Android 通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我正在开发的 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.
对于 Android 4.2 及更低版本:
您需要注册一个 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;
}
}
对于 Android 4.3 及更高版本:
这篇关于检测新的 Android 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文