I have resolved this issue. see code below:
public class MyTest extends TestCase{
/**
* Paste text to an EditText feild which is currentlly get focused.
*
* @param: text the text(Non-ASCII) you want to paste into EditText feild.
*/
IClipboard clipboard = IClipboard.Stub.asInterface(ServiceManager.getService(Context.CLIPBOARD_SERVICE));
IInputManager iInputManager = IInputManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_SERVICE));
private void pastText(String text) throws UiObjectNotFoundException{
try {
//copy the text to clipboard.
clipboard.setPrimaryClip(ClipData.newPlainText("NonASCII", text), text);
//inject event: press Menu + V
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1);
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_V, 0),1);
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_V, 0),1);
//After "Menu"+"V" pressed, A "Menu" will show if exist in current Activicy.
//Then press menu again, to make it down just for bug fixing.
sleep(300);
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1);
iInputManager.injectInputEvent(
new KeyEvent(android.os.SystemClock.uptimeMillis(),
android.os.SystemClock.uptimeMillis(),
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Thanks.