androidxamarin.androidaccessibilityservice

Xamarin.Android AccessibilityServiceInfo does not load configs from xml


I need to retrieve the event source in accessibility service. To do that I have to add canRetrieveWindowContent="true" to the accessibilityServiceInfo xml. It works in java/android studio but in xamarin.android/VS it does not want to get the configs from the xml and I can't figure out why. If I rename the xml I get file not found error so it must load the xml just doesn't use it for accessibilityServiceInfo.

My xml (xml/accessibility_service_config.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:canRequestFilterKeyEvents="true"
   />

My init:

[Service(Label = "myApp", Permission = Manifest.Permission.BindAccessibilityService)]
[IntentFilter(new[] { "android.accessibilityservice.AccessibilityService" })]
[MetaData("android.accessibilityservice.AccessibilityService", Resource = "@xml/accessibility_service_config")]
public class Receiver1 : AccessibilityService
{

    protected override void OnServiceConnected()
    {
        base.OnServiceConnected();

        AccessibilityServiceInfo info = ServiceInfo;
        info.EventTypes = EventTypes.AllMask;
        info.FeedbackType = Android.AccessibilityServices.FeedbackFlags.Spoken;
        info.NotificationTimeout = 500;
        info.Flags |= AccessibilityServiceFlags.Default;
        info.Flags |= AccessibilityServiceFlags.ReportViewIds;

        SetServiceInfo(info);
    }

When I place breakpoint just after AccessibilityServiceInfo info = ServiceInfo; I see the configs are not loaded regardless what I put in the xml.


Solution

  • You can change the code [MetaData("android.accessibilityservice.AccessibilityService", Resource = "@xml/accessibility_service_config")] to the [MetaData("android.accessibilityservice", Resource = "@xml/accessibility_service_config")]. The first parameter is the name of the metadata, and it should obey the rules in android.