Reputation: 6839
I have made use of Accessibility service in android to detect the foreground application.
I want to listen to 2 of the available accessibility events
TYPE_VIEW_TEXT_CHANGED
TYPE_WINDOW_STATE_CHANGED
I want to listen to all window state change
events, that is working, but for second event, i want to listen to view_text_changed
event of only 1-2 applications and not all.
I have read & tried the android:packageNames
parameter in xml, but then it will impose limitation on window_state_changed event.
Is there any other way to do that??
Upvotes: 2
Views: 1665
Reputation: 2405
Assuming you have setup your AndroidManifest
to recognise an AccessibilityService
, I am adding the other required code here just in case someone wants a direct answer.
Accessibility service configuration file:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android= "http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeWindowStateChanged|typeViewTextChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
Note the android:accessibilityEventTypes
are as needed. If you limit the package in xml you won't get the TYPE_WINDOW_STATE_CHANGED
as rightly pointed out in the question.
So, you also need to make changes in your implementation of the AccessibilityService
.
public class VoiceAccessibilityService extends android.accessibilityservice.AccessibilityService {
private static String TAG = VoiceAccessibilityService.class.getName();
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED && event.getPackageName().equals("Your package names")) {
Logger.d(TAG + " SHOW " + event.toString());
}
}
@Override
public void onInterrupt() {
Logger.d(TAG + " interrupt");
}
}
Hope it helps!
Upvotes: 1
Reputation: 1711
Unfortunately there isn't way to specify interesting package names for specified accessibility events. You have to just use AccessibilityEvent#getPackageName to filter interesting you packages.
Upvotes: 1