a-python-script
a-python-script

Reputation: 898

Android onAccessibilityEvent in AccessibilityService not getting called

I try to develop a very simple scenario where I just get a small toast message when an event occurs. Unfortunately, I can't find out why the code below doesn't open a toast message. I know there are already some posts on the subject that I have all read and none of them have been able to help me. I would be very grateful if someone could help me or provide a working example.

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.testing.attack">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service android:name=".MaliciousService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
            android:label="A malicious service">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService"/>
            </intent-filter>
        </service>
    </application>

</manifest>

The service

package com.example.android.testing.attack;

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

public class MaliciousService extends AccessibilityService {

    @Override
    public void onServiceConnected() {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.notificationTimeout = 100;
        this.setServiceInfo(info);
        Toast t = Toast.makeText(getApplicationContext(), "Malicious Accessibility Service is connected now", Toast.LENGTH_SHORT);
        t.show();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Toast t = Toast.makeText(getApplicationContext(), "An event occurred", Toast.LENGTH_SHORT);
        t.show();
    }

    @Override
    public void onInterrupt() {
        Toast t = Toast.makeText(getApplicationContext(), "An interrupt occurred", Toast.LENGTH_SHORT);
        t.show();
    }
}

I turn on the service in the settings and I get the initial Toast which says "Malicious Service is connected now". But I dont get any other Toast whenever I swipe or open a new activity or something else.

Upvotes: 2

Views: 2609

Answers (2)

a-python-script
a-python-script

Reputation: 898

Based on Raj's commentary, I was able to develop the following solution:

@Override
    public void onServiceConnected() {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.notificationTimeout = 100;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        this.setServiceInfo(info);
        Toast t = Toast.makeText(getApplicationContext(), "Malicious Accessibility Service is connected now", Toast.LENGTH_SHORT);
        t.show();
    }

All I had to do was add the FEEDBACK_ALL_MASK configuration. Unfortunately, I couldn't find any exact details in the documentation what exactly is controlled with this configuration.

Upvotes: 5

Salman
Salman

Reputation: 9

You have to show toast on main thread. If you are using toast just for debug than please use Log.

Or

Please read this

Android Service to show toast

Upvotes: 0

Related Questions