os:mtk 8.1
做一个扫码,当前具有焦点的输入框自动填充内容的功能。
1.通过广播
- /frameworks/base/core/java/android/inputmethodservice/InputMethodService.java
@Override
public void onCreate() {
...
initViews();
mWindow.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT);
// 注册广播
IntentFilter scanFilter = new IntentFilter();
registerReceiver(scanReciever,scanFilter );
}
//添加自定义广播
private BroadcastReceiver scanReciever = new BroadcastReceiver () {
@Override
public void onReceive(Context context, Intent intent) {
InputConnection conn = getCurrentInputConnection();
String intent_data=intent.getStringExtra("data");
if (conn != null) {
conn.commitText(intent_data,intent_data.length());
}
}
}
2.添加自定义方法
- frameworks/base/core/java/com/android/internal/view/IInputMethodManager.aidl
...
// 添加自定义方法
void showTextFromUart(String data);
}
- frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
public void showTextFromUart(String uartData){
try {
mService.showTextFromUart(uartData);
} catch (RemoteException e) {
Log.e(TAG, "showTextFromUart failed " +e);
}
}
- frameworks/base/services/core/java/com/android/server/InputMethodManagerService.java
public void showTextFromUart(String data){
System.out.println("showTextFromUart "+mCurInputContext+" data ="+data);
if (mCurInputContext != null) {
try {
mCurInputContext.commitText(data,data.length());
} catch (RemoteException e){
Slog.e(TAG," commitText failed "+e);
}
}
}
调用
InputMethodManager manager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager != null) {
manager.showTextFromUart(msg.obj.toString());
}