Android accessibility service


Answers

Accessibility Service is overkill for your purposes, you should use NotificationListenerService. The NotificationListenerService is much easier to implement and safer for users (privacy reasons).

Question

I'm trying to write a very basic android accessibility service that shows a message and vibrates when any notification is raised. I've tried testing it by sending an email myself on my phone (I figured that would show some notification). But I have not seen any notifications.

My service code looks like

public class NotifierService extends AccessibilityService {

  @Override
  public void onAccessibilityEvent(AccessibilityEvent evt) {
    Toast.makeText(this, "Got event from " + evt.getPackageName(), Toast.LENGTH_SHORT)
            .show();
    Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    v.vibrate(new long[] { 0, 250, 250, 250, 250, 250, 250, 250, 250 }, -1);
  }

  @Override
  public void onInterrupt() { }
}

I have verified that the service is running and I have enabled it in the accessibility menu of the phone. And the manifest looks like this (some parts are removed that are not relevant):

<uses-permission android:name="android.permission.VIBRATE" />
<application>
    <activity
        android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".NotifierService" android:enabled="true" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
    </service>
</application>

MyActivity is just an activity with a button for starting/stopping the service.




onAccessibilityEvent(AccessibilityEvent event) not intercepting notification

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








Tags