Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Copy & Paste to any text field in any application

I am developing an app for commercial use with a background service that is getting transponder numbers (of animals) from an RFID reader via bluetooth.

After processing the received number I would like to send it to the clipboard and paste it in the focused text field of whatever application is currently in front which in my case is a browser app.

I already found a similar question from 2013 but with no accepted answer by now. All answers to the question just explained how to use ClipboardManager to copy and paste code within the developed application but that has not been meant by the problem as he clarified in a comment.

The simplest scenario that I could imagine is to just simulate a paste action on the android device. I would prefer not to need to install a third party app.

like image 503
markus s Avatar asked Jun 01 '16 06:06

markus s


People also ask

Where is copied stored on Android?

Look for a clipboard icon in the top toolbar. This will open the clipboard, and you'll see the recently copied item at the front of the list. Simply tap any of the options in the clipboard to paste it into the text field.


1 Answers

Just to add to Kirill's answer and assuming the app has Accessibility permission,

Create a class extending AccessibilityService and override onAccessibilityEvent method.

public class SampleAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        AccessibilityNodeInfo source = accessibilityEvent.getSource();
        if (source != null) {
            AccessibilityNodeInfo rowNode = getRootInActiveWindow();
            if (rowNode != null) {
                for (int i = 0; i < rowNode.getChildCount(); i++) {
                    AccessibilityNodeInfo accessibilityNodeInfo = rowNode.getChild(i);
                    if (accessibilityNodeInfo.isEditable() && accessibilityNodeInfo.isFocused()) {
                        accessibilityNodeInfo.performAction(AccessibilityNodeInfoCompat.ACTION_PASTE);
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void onInterrupt() {

    }
}

accessibilityNodeInfo.performAction(AccessibilityNodeInfoCompat.ACTION_PASTE) will paste the text that is copied to clipboard.

Also make sure you have right accessibility configuration.

config.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityFlags="flagDefault"
    android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:notificationTimeout="0"
    android:canRetrieveWindowContent="true"
    android:description="@string/testing" />

Here android:accessibilityEventTypes="typeViewClicked|typeViewFocused" will filter the events to view click or view focus.

You can also the events based on the packages using "android:packageNames" (so that your service won't get called often)

Finally declare the service in manifest,

<service android:name=".SampleAccessibilityService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService"/>
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/config" />
</service>
like image 198
Govind Avatar answered Sep 28 '22 17:09

Govind