onAccessibilityEvent(AccessibilityEvent event) not

2019-04-02 05:50发布

问题:

In my application I have to catch the new incoming email massage notification and play a video on the screen which describe the event. I'm using the onAccessibilityEvent(AccessibilityEvent event) to listen to the generated notification. I also use the following code for init my AccessibilityService:

    AccessibilityServiceInfo localAccessibilityServiceInfo = new AccessibilityServiceInfo();
    localAccessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; // AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    localAccessibilityServiceInfo.notificationTimeout = EVENT_NOTIFICATION_TIMEOUT_MILLIS;
    localAccessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_AUDIBLE; // AccessibilityServiceInfo.FEEDBACK_VISUAL;
    localAccessibilityServiceInfo.flags &= AccessibilityServiceInfo.DEFAULT;
    setServiceInfo(localAccessibilityServiceInfo);

but event never arrived. How it can be solved? Thanks, Eyal.

回答1:

I have a similar situation, this works in my application:

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

public class NotificationAccessibilityService extends AccessibilityService
{
    public static final String TAG = "volumeMaster";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event)
    {
        Log.v(TAG, "***** onAccessibilityEvent");
        Toast.makeText(getApplicationContext(), "Got event from: " + event.getPackageName(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInterrupt()
    {
        Log.v(TAG, "***** onInterrupt");
    }

    @Override
    public void onServiceConnected()
    {
        Log.v(TAG, "***** onServiceConnected");

        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.notificationTimeout = 100;
        info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
        setServiceInfo(info);

    }
}

And in my manifest:

<service android:name=".accessibility.NotificationAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>

</service>

Parts of my solution came from: Accessibility Service from StackOverflow