event.getSource() returns null Accessibility in an

2019-07-24 20:14发布

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

2条回答
贼婆χ
2楼-- · 2019-07-24 20:15

Make sure android:canRetrieveWindowContent="true"

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

查看更多
Deceive 欺骗
3楼-- · 2019-07-24 20:30

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

       <meta-data android:name="android.accessibilityservice"
       android:resource="@xml/accessibility">
查看更多
登录 后发表回答