7

I have been working on an app which displays all the notifications in the phone's status bar (like a new msg or a missed call). By researching I found out that the only way to do that in android is to create an accessibility service. So I did the same by getting AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED in my accessibility service.
Here is my onServiceConnected() method:

public 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;
}

This is working but not exactly I wish. Its actually showing many notifications which are not there in status bar and are being probably generated by background apps which i don want. Also its not real time. It updates only when I re-open the app and not while the app is open. Furthermore, as soon as I start the service in settings, my phone(galaxy note) starts talking everything. Not sure if it's a bug or some problem in the code.
I can't figure out where exactly the problem is but I think it can be the feedback type. I tried a lot but couldn't find out the meaning of different feedback types for the service anywhere.

FEEDBACK_AUDIBLE
FEEDBACK_HAPTIC
FEEDBACK_AUDIBLE
FEEDBACK_VISUAL
FEEDBACK_GENERIC

Can someone please tell me what does these feedback types imply? And would I get different notifications if I change the feedback type? And if you believe I'm heading in the wrong direction then please guide me to what I can do to solve these problems. Lemme know if I forgot to provide some essential info.

Any kind of help would be really appreciated.

Anjani
  • 1,533
  • 3
  • 15
  • 26

1 Answers1

1

Well I don't have answers to all your questions but I can help you with something. As you mentioned that you want only statusbar notifications and not others like toasts and all. So for that purpose, you can try the following code in your service.

public void onAccessibilityEvent(AccessibilityEvent event) {
    final int eventType = event.getEventType();
    if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        Parcelable parcelable = event.getParcelableData();

        if(parcelable instanceof Notification){
            // statusbar notification
            List<CharSequence> messages = event.getText();
            if (messages.size() > 0) {
                // here you can do whatever you want with the notification.
                //Log.d(tag, "Saved event");
            }
        }
    }
}

Also set a timeout in your onServiceConnected like this:

info.notificationTimeout=100;  

I hope it works for you.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Its did help me fetch statusbar notifications. Thank you for answering. The major problem am facing right now is my talking phone as I mentioned earlier. I think its a bug in android. I'd really appreciate if you can help me with that. – Anjani Nov 14 '13 at 08:56
  • Sounds like you've switched the TalkBack Accessibility service on in Accessibility settings? – tommed Jan 20 '16 at 07:39
  • your code was helpful but i'm facing a problem here. even this code didn't solve it. what i'm facing is that whenever a notification comes in the notification status bar. my `onAccessibilityEvent(AccessibilityEvent)` method gets called twice, and i've checked it on many devices. it gets called twice. so whatever i do in `onAccessibilityEvent(AccessibilityEvent)` is repeated twice due to this. Can someone help me resolve this? – Akash Raghav Jan 22 '16 at 13:42
  • I am able to capture the incoming ussd message, but how to compare the incoming ussd message in case of dual sim phone? means if i receive a ussd message alert then how i can know that incoming ussd message is for which sim? – Sachin Arora Jun 04 '16 at 12:18
  • @AkashRaghav I'm facing the same issue, some notifications causes the `onAccessibilityEvent` to trigger twice, did you find a solution to this? – Denny Mar 29 '17 at 22:29