So I got my AccessibilityService working with the following code:
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
List<CharSequence> notificationList = event.getText();
for (int i = 0; i < notificationList.size(); i++) {
Toast.makeText(this.getApplicationContext(), notificationList.get(i), 1).show();
}
}
}
It works fine for reading out the text displayed when the notifcation was created (1).
The only problem is, I also need the value of (3) which is displayed when the user opens the notification bar. (2) is not important for me, but it would be nice to know how to read it out. As you probably know, all values can be different.
So, how can I read out (3)? I doubt this is impossible, but my notificationList
seems to have only one entry (at least only one toast is shown).
Thanks a lot!
/edit: I could extract the notification parcel with
if (!(parcel instanceof Notification)) {
return;
}
final Notification notification = (Notification) parcel;
However, I have no idea how to extract the notifcation's message either from notification
or notification.contentView
/ notification.contentIntent
.
Any ideas?
/edit: To clarify what is asked here: How can I read out (3)?
I've wasted a few hours of the last days figuring out a way to do what you (and, me too, by the way) want to do. After looking through the whole source of RemoteViews twice, I figured the only way to accomplish this task is good old, ugly and hacky Java Reflections.
Here it is:
This code worked fine on a Nexus S with Android 4.0.3. However, I didn't test if it works on other versions of Android. It's very likely that some values, especially the viewId changed. I think there should be ways to support all versions of Android without hard-coding all possible ids, but that's the answer to another question... ;)
PS: The value you're looking for (referring to as "(3)" in your original question) is the "text"-value.
I've spent the last week working with a similar problem and can propose a solution similar to Tom Tache's (using reflection), but might be a little bit easier to understand. The following method will comb a notification for any text present and return that text in an ArrayList if possible.
Because this probably looks a bit like black magic, let me explain in more detail. We first pull the RemoteViews object from the notification itself. This represents the views within the actual notification. In order to access those views, we either have to inflate the RemoteViews object (which will only work when an activity context is present) or use reflection. Reflection will work in either circumstance and is the method used here.
If you examine the source for RemoteViews here, you will see that one of the private members is an ArrayList of Action objects. This represents what will be done to the views after they are inflated. For example, after the views are created, setText() will be called at some point on each TextView that is a part of the hierarchy to assign the proper Strings. What we do is obtain access to this list of actions and iterate through it. Action is defined as follows:
There are a number of concrete subclasses of Action defined in RemoteViews. The one we're interested in is called ReflectionAction and is defined as follows:
This action is used to assign values to views. A single instance of this class would likely have the values {"setText", 10, "content of textview"}. Therefore, we're only interested in the elements of mActions that are "ReflectionAction" objects and assign text in some way. We can tell a particular "Action" is a "ReflectionAction" by examining the "TAG" field within the Action, which is always the first value to be read from the parcel. TAGs of 2 represent ReflectionAction objects.
After that, we just have to read the values from the parcel according to the order in which they were written (see the source link, if you're curious). We find any string that is set with setText() and save it in the list. (setTime() is also included, in case the notification time is also needed. If not, those lines can be safely deleted.)
While I typically oppose the use of reflection in instances like this, there are times when it is necessary. Unless there is an activity context available, the "standard" method won't work properly, so this is the only option.
To answer your question: This does not seem possible in your case. Below I explain why.
"The main purpose of an accessibility event is to expose enough information for an AccessibilityService to provide meaningful feedback to the user." In cases, such as yours:
We can request this privilege explicitly by setting meta data for the service in your android manifest file:
Where your xml file could look like:
We explicitly request the privilige for accessing an event's source (the window content) and we specify (using
accessibilityEventTypes
) the event types this service would like to receive (in your case onlytypeNotificationStateChanged
). See AccessibilityService for more options which you can set in the xml file.Normally (see below why not in this case), it should be possible to call
event.getSource()
and obtain aAccessibilityNodeInfo
and traverse through the window content, since "the accessibility event is sent by the topmost view in the view tree".While, it seems possible to get it working now, further reading in the AccessibilityEvent documentation tells us:
Apparently, this is because of security purposes...
To hook onto how to extract the notifcation's message either from notification or notification.contentView / notification.contentIntent. I do not think you can.
The contentView is a RemoteView and does not provide any getters to obtain information about the notification.
Similarly the contentIntent is a PendingIntent, which does not provide any getters to obtain information about the intent that will be launched when the notification is clicked. (i.e. you cannot obtain the extras from the intent for instance).
Furthermore, since you have not provided any information on why you would like to obtain the description of the notification and what you would like to do with it, I cannot really supply you with a solution to solve this.
If you've tried TomTasche's solution on Android 4.2.2, you'll notice it doesn't work. Expanding on his answer, and user1060919's comment... Here is an example that works for 4.2.2:
Achep's AcDisplay project has provided a solution, check the code from Extractor.java
Adding on Remi's answer, to identify different notification types and extract data, use the below code.