event.getSource() returns null Accessibility in android

柔情痞子 提交于 2019-12-14 02:18:52

问题


I try to fill a EditText field by using a accessibility service like DashLane or some other apps which are using accessibility service.

I am using an event view focused. When EditText gets focused the event getting started but getSource() returns null.

Code:

  AccessibilityNodeInfo source = event.getSource();

if (source != null) {
        ClipboardManager clipboard = (ClipboardManager)  context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", "TEST DATA");
        clipboard.setPrimaryClip(clip);
        source.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        Toast.makeText(MyAccessibilityService.this, "Pasted Successfully", Toast.LENGTH_LONG);
}

... and I've tried some of the other methods which is giving errors. So one question here is why does it return null?

Androidmanifest

            <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
            <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
            <application
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name" 
                android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
                <service android:name=".MyAccessibilityService" android:label="@string/app_name" android:enabled="true" android:exported="true">
                <intent-filter>
                    <action android:name="android.accessibilityservice.AccessibilityService" android:resource="@xml/accessibility" />
                </intent-filter>  

Accessibility.xml

        <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
            android:description="@string/accessibility_service_description"
            android:packageNames="com.test.toasts2"
            android:accessibilityEventTypes="typeAllMask"
            android:accessibilityFlags="flagDefault"
            android:accessibilityFeedbackType="feedbackSpoken"
            android:notificationTimeout="100"
            android:canRetrieveWindowContent="true"
            android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
        />

回答1:


You missed the meta tag in manifest file. just add.

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



回答2:


Make sure android:canRetrieveWindowContent="true"

This worked for me. And this property can not be set by java code.



来源:https://stackoverflow.com/questions/30864868/event-getsource-returns-null-accessibility-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!