From my MainActivity I'm using AccessibilityService to perform click event on another app and it works when I open the app and it is displayed on the screen. However I want to perform the click event without having to open the app, it can be opened but not currently displayed on the screen.
Is this possible?
public class DiaryAppAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
}
@Override
public void onAccessibilityEvent (AccessibilityEvent event) {
if (getRootInActiveWindow () == null) {
return;
}
AccessibilityNodeInfoCompat rootInActiveWindow = AccessibilityNodeInfoCompat.wrap (getRootInActiveWindow());
List<AccessibilityNodeInfoCompat> messageNodeList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.diaryapp:id/entry");
if (messageNodeList == null || messageNodeList.isEmpty ()) {
return;
}
AccessibilityNodeInfoCompat messageField = messageNodeList.get (0);
if (messageField.getText () == null || messageField.getText ().length () == 0
|| !messageField.getText ().toString ().endsWith (getApplicationContext ().getString (R.string.diaryapp_suffix))) { // So your service doesn't process any message, but the ones ending your apps suffix
return;
}
List<AccessibilityNodeInfoCompat> sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId ("com.diaryapp:id/send");
if (sendMessageNodeInfoList == null || sendMessageNodeInfoList.isEmpty ()) {
return;
}
AccessibilityNodeInfoCompat sendMessageButton = sendMessageNodeInfoList.get (0);
// Now fire a click on the send button
sendMessageButton.performAction (AccessibilityNodeInfo.ACTION_CLICK);
@Override
public void onInterrupt() {
}
}
XML config
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowContentChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:accessibilityFlags="flagDefault"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
</accessibility-service>
I removed this
android:packageNames="com.diaryapp"
because I want the service to be active in other apps and be able to perform the click event from there.
What you're trying to do is not possible by design. The Accessibility API is intended to help people with disabilities interact with the UI shown on the phone.