原创文章,转载请标注出处----
我们知道当一个编辑框获得焦点的时候,将会create一个新的IME客户端,那么IMF框架是怎样处理这个事件的呢?
首先调用EditText的构造函数EditTex->TextView->setText()
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
->startInputInner
void startInputInner() {
final View view;
synchronized (mH) {
view = mServedView;
// Make sure we have a window token for the served view.
if (DEBUG) Log.v(TAG, "Starting input: view=" + view);
if (view == null) {
if (DEBUG) Log.v(TAG, "ABORT input: no served view!");
return;
}
}
// Now we need to get an input connection from the served view.
// This is complicated in a couple ways: we can't be holding our lock
// when calling out to the view, and we need to make sure we call into
// the view on the same thread that is driving its view hierarchy.
Handler vh = view.getHandler();
if (vh == null) {
// If the view doesn't have a handler, something has changed out
// from under us, so just bail.
if (DEBUG) Log.v(TAG, "ABORT input: no handler for view!");
return;
}
if (vh.getLooper() != Looper.myLooper()) {
// The view is running on a different thread than our own, so
// we need to reschedule our work for over there.
if (DEBUG) Log.v(TAG, "Starting input: reschedule to view thread");
vh.post(new Runnable() {
public void run() {
startInputInner();
}
});
return;
}
// Okay we are now ready to call into the served view and have it
// do its stuff.
// Life is good: let's hook everything up!
EditorInfo tba = new EditorInfo();
tba.packageName = view.getContext().getPackageName();
tba.fieldId = view.getId();
InputConnection ic = view.onCreateInputConnection(tba); //create 新的InputConnection
if (DEBUG) Log.v(TAG, "Starting input: tba=" + tba + " ic=" + ic);
synchronized (mH) {
// Now that we are locked again, validate that our state hasn't
// changed.
if (mServedView != view || !mServedConnecting) {
// Something else happened, so abort.
if (DEBUG) Log.v(TAG,
"Starting input: finished by someone else (view="
+ mServedView + " conn=" + mServedConnecting + ")");
return;
}
// If we already have a text box, then this view is already
// connected so we want to restart it.
final boolean initial = mCurrentTextBoxAttribute == null;
// Hook 'em up and let 'er rip.
mCurrentTextBoxAttribute = tba;
mServedConnecting = false;
mServedInputConnection = ic;
IInputContext servedContext;
if (ic != null) {
mCursorSelStart = tba.initialSelStart;
mCursorSelEnd = tba.initialSelEnd;
mCursorCandStart = -1;
mCursorCandEnd = -1;
mCursorRect.setEmpty();
servedContext = new ControlledInputConnectionWrapper(vh.getLooper(), ic);
} else {
servedContext = null;
}
try {
if (DEBUG) Log.v(TAG, "START INPUT: " + view + " ic="
+ ic + " tba=" + tba + " initial=" + initial);
InputBindResult res = mService.startInput(mClient,
servedContext, tba, initial, mCurMethod == null); //启动IMEservice
if (DEBUG) Log.v(TAG, "Starting input: Bind result=" + res);
if (res != null) {
if (res.id != null) {
mBindSequence = res.sequence;
mCurMethod = res.method;
} else {
// This means there is no input method available.
if (DEBUG) Log.v(TAG, "ABORT input: no input method!");
return;
}
}
if (mCurMethod != null && mCompletions != null) {
try {
mCurMethod.displayCompletions(mCompletions);
} catch (RemoteException e) {
}
}
} catch (RemoteException e) {
Log.w(TAG, "IME died: " + mCurId, e);
}
}
那么到了IME部分的流程又是如何的呢?
首先收到configure change的event调用onConfigurationChanged->inputmethodservice.onConfigurationChanged(conf)
@Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
boolean visible = mWindowVisible;
int showFlags = mShowInputFlags;
boolean showingInput = mShowInputRequested;
CompletionInfo[] completions = mCurCompletions;
initViews();
mInputViewStarted = false;
mCandidatesViewStarted = false;
if (mInputStarted) {
doStartInput(getCurrentInputConnection(),
getCurrentInputEditorInfo(), true); //调用startinput,创建IME的input
}
if (visible) {
if (showingInput) {
// If we were last showing the soft keyboard, try to do so again.
if (onShowInputRequested(showFlags, true)) {
showWindow(true);
if (completions != null) {
mCurCompletions = completions;
onDisplayCompletions(completions);
}
} else {
hideWindow();
}
} else if (mCandidatesVisibility == View.VISIBLE) {
// If the candidates are currently visible, make sure the
// window is shown for them.
showWindow(false);
} else {
// Otherwise hide the window.
hideWindow();
}
}
}
doStartInput->initialize->onInitializeInterface->onStartInput->onStartInputView.
onStartInputView中会handle各种IME的event并进一步调用IME自身的onCreate函数,做进一步的初始化以及receiver的rigister。
分享到:
相关推荐
Android Framework精编内核解析
android framework 实现细节,常见面试问题汇总
This is a android framework full classes jar file
android Framework精编内核解析
Android Framework精编内核解析.pdf 百度网盘永久地址
1.初探Android 2.Android开发环境搭建与编译 3.Android编译过程分析 4.android.mk初识 5.Android.mk学习1 6.Android.mk学习2 7.Android.mk学习3 8.Android启动课程大纲 9.Android启动模式 10.init启动分析 11.走入...
android framework相关面试题,详细深入的介绍了android framework相关知识点,可以很好帮助对android framework的深入理解
android framework 源代码
android framework开发经验总结
Android 13.0 framework jar 包
Android Framework 分析Android Framework 分析Android Framework 分析Android Framework 分析
自己编译的 Android12版本-framework源码jar 来源:AOSP源分支 android-12.1.0_r11
android framework面试题集 自己亲自总结的,各位有需要的可以随时下载
android framework层源码解析
《Android Framework 开发揭秘》,系统框架底层原理解密,腾讯技术团队最新出品!
Android11.0 系统分析 Android11.0 启动分析 Android11.0 最新Framework解析 Android11.0 Binder通信原理
android Framework层的修改规则,包括了编译过程和jni调用等。
Android Framework Source Code,下载并解压后,将解压出的整个文件夹复制或者移动到.../android-sdk-windows/sources文件夹下,然后重新打开SDK Manager.exe。
系统包 framework.jar ,可以实现隐藏方法的调用!以前写系统应用的时候用到过,之后找不到了,备份一下以免以后丢失
android framework 详解