Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a custom event to AccessibilityService?

My program purpose: trigger the BACK button in a service

I tried many ways, no one can achieve this purpose, finally I discovered AccessibilityService, it may be the most possible ways to implement this function.

I created this AccessibilityService, and tested it is work

package com.accessibilityservice;

public class MyAccessibilityService extends AccessibilityService {
    public MyAccessibilityService() {
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);

    }

    @Override
    public void onInterrupt() {

    }
}

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackSpoken"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:description="desc"
    android:notificationTimeout="100"
    android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" />

And then I tried to move performGlobalAction to service, but it does not perform the action.

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyAccessibilityService mas=new MyAccessibilityService();
        mas.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    }
}

I also tried to send a custom event in different way, but no one can send to MyAccessibilityService

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //method1
    AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_CLICKED);
    event.setContentDescription("this is description");
    View view = new View(this);
    ViewParent parent = view.getParent();
    if (parent != null) {
        parent.requestSendAccessibilityEvent(view, event);
    }

    //method2
    AccessibilityManager manager = (AccessibilityManager) getApplicationContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    AccessibilityEvent event = AccessibilityEvent.obtain();
    event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
    event.setPackageName("p");
    event.setClassName("c");
    manager.sendAccessibilityEvent(event);
}

How can I send a custom event or message to MyAccessibilityService, so that I can recognize the event and message to perform the action?

like image 416
CL So Avatar asked Mar 02 '16 05:03

CL So


1 Answers

1 Create a static field of your service

public static MyAccessibilityService instance;

2 then initialize it in onServiceConnected()

    @Override
    protected void onServiceConnected() {
    super.onServiceConnected();
    instance = this;
}

3 don't foget to clear it in onUnbind()

    @Override
    public boolean onUnbind(Intent intent) {
    instance = null;
    return super.onUnbind(intent);
}

onServiceConnected() is called by the System when user gives permission to your app in device Settings

onUnbind() is when users revokes that permission

4 Use your instance everywhere you want

if(MyAccessibilityService.instance != null)
    MyAccessibilityService.instance.performGlobalAction(....)
like image 133
Kirill Karmazin Avatar answered Sep 29 '22 02:09

Kirill Karmazin