8

I am developing an Android app, where I have to send text to focused EditText field of other Applications.

I have done it in API level 18 and above using AccessibilityService, when I find the EditText, I paste the data from ClipBoard. Here is the code,

public class TestService extends AccessibilityService {

  @Override
  public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo source = event.getSource();
        if (source != null && event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED && event.getClassName().equals("android.widget.EditText")) {
                ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("label", "TEST DATA");
                clipboard.setPrimaryClip(clip);
                source.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        }
   }
}

But, AccessibilityNodeInfo.ACTION_PASTE is available in API level 18 and above.

Can we achieve the same in API Level < 18 ?

I read in some blogs that it can be done using InputMethodService, where we create our own keyboard and send text via that soft keyboard. I did not understand much of it...

can someone help me on this.

Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
amithgc
  • 6,167
  • 6
  • 29
  • 38

3 Answers3

1

You can use ACTION_PASTE from API 14, but you have to call AccessibilityRecordCompat and AccessibilityNodeInfoCompat like there:

AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
AccessibilityNodeInfoCompat source = record.getSource();
source.performAction(AccessibilityNodeInfo.ACTION_PASTE);
Anton Kashpor
  • 1,255
  • 2
  • 18
  • 34
0

You could get the view that has the current focus:

public View getCurrentFocus() {
    return mWindow != null ? mWindow.getCurrentFocus() : null;
}

And then setting up the text:

editTextObject.setText(CharSequence)
kikoso
  • 673
  • 1
  • 6
  • 17
  • I have to send text to focused EditText field of other Applications. How do i get the activity instance? not possible right.. – amithgc Apr 28 '14 at 11:58
0

Yes you can use AccessibilityNodeInfo even from API 14 and up but ACTION_PASTE you have to use only from Api Level 18. Have a look at http://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html

Ajit Kumar
  • 534
  • 3
  • 8