由网友(执子之手,将子拖走)分享简介:在Android的应用程序,我的工作,我想,当一个新的状态栏通知出现,不管它是否是由我的应用程序引起的,就能够检测到。更具体地,我要计数在给定时间帧通知的数量。In the Android app that I'm working on, I'd like to be able to detect when a ne...
在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.
您需要注册一个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;
}
}
Example检查如果您的服务已激活
相关推荐
最新文章