I implement an input method called RemoteInput simply extend InputMethodService, without InputViews and no keyboards. When user select RemoteInput as default IME, RemoteInput will send the current input status to other device and user can do input action remotely(using our customerized protocol). When input done, the text entered in other device will be sent back to current device, then RemoteInput commit the text onto current UI component(such as EditText) using InputConnection.commitText (CharSequence text, int newCursorPosition)
.
It works perfect when remote-inputed text are English characters and numbers, but when it comes to other characters things go wrong. I have found that InputConnection.commitText
filter other characters. For instance, I enterd hello你好
, only hello
be commited successfully. And more:
hello world
==>helloworld
hello,world!!
==>helloworld
Anything you tell about this will be helpful, thanks in advance.
Here is my code:
public class RemoteInput extends InputMethodService {
protected static String TAG = "RemoteInput";
public static final String ACTION_INPUT_REQUEST = "com.aidufei.remoteInput.inputRequest";
public static final String ACTION_INPUT_DONE = "com.aidufei.remoteInput.inputDone";
private BroadcastReceiver mInputReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_INPUT_DONE.equals(intent.getAction())) {
String text = intent.getStringExtra("text");
Log.d(TAG, "broadcast ACTION_INPUT_DONE, input text: " + text);
input(text);
}
}
};
@Override
public void onCreate() {
super.onCreate();
registerReceiver(mInputReceiver, new IntentFilter(ACTION_INPUT_DONE));
}
@Override
public View onCreateInputView() {
//return getLayoutInflater().inflate(R.layout.input, null);
return null;
}
@Override
public boolean onShowInputRequested(int flags, boolean configChange) {
if (InputMethod.SHOW_EXPLICIT == flags) {
Intent intent = new Intent(ACTION_INPUT_REQUEST);
getCurrentInputConnection().performContextMenuAction(android.R.id.selectAll);
CharSequence text = getCurrentInputConnection().getSelectedText(0);
intent.putExtra("text", text==null ? "" : text.toString());
sendBroadcast(intent);
}
return false;
}
public void input(String text) {
InputConnection inputConn = getCurrentInputConnection();
if (text != null) {
inputConn.deleteSurroundingText(100, 100);
inputConn.commitText(text, text.length());
}
//inputConn.performEditorAction(EditorInfo.IME_ACTION_DONE);
}
@Override
public void onDestroy() {
unregisterReceiver(mInputReceiver);
super.onDestroy();
}
}