Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

notifications - Android accessibility service

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...