Android accessibility service
Asked Answered
E

3

4

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.

Evaginate answered 12/3, 2012 at 22:18 Comment(4)
Since you don't have the <meta-data> element, are you calling setServiceInfo() somewhere, per the docs on AccessibilityService? I haven't implemented one of these, so I'm just taking an educated guess.Piselli
Ah, I'm not. I'll give that a try and see what happens.Evaginate
Can you post that as an answer? :)Friedcake
@KaolinFire I posted the answerEvaginate
E
4

I added a meta data element inside the service tag

<meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />

And in the xml resources file I have

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
                       android:accessibilityEventTypes="typeNotificationStateChanged"
                       android:accessibilityFeedbackType="feedbackAllMask"
                       android:notificationTimeout="100" />
Evaginate answered 26/1, 2013 at 18:14 Comment(1)
Hi Jueff, why it's not triggered when the notification is dismissed? It's only triggered when notification is created. Thank you.Undertook
D
1

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

Disqualification answered 9/12, 2015 at 19:56 Comment(0)
D
0

One other way to configure the accessibility service is to Override the OnServiceConnected() method of your AccessibilityService from java code.

@Override
  protected void onServiceConnected() {
    AccessibilityServiceInfo info = getServiceInfo();
    info.packageNames = new String[]
        {
            "com.packagename1","com.packagename2"
        };
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;

    this.setServiceInfo(info);
  }

Reference

Disdain answered 22/6, 2016 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.