A keyboard listener for Android which by hooking the InputMethodManager. Of course you can also hook the other system services similarly, If you want, create a class, make it a subclass of
Hook
, and usingServiceManagerHook
to hook ServiceManager, learn more fromInputMethodManagerHook
. If you like this project, ,welcome to fork or star!
1.Import InputMethodHodler as a library
2.Call the initialization method, the method will hook InputMethodManager, recommended to call at attachBaseContext
:
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
InputMethodHolder.init(base);
super.attachBaseContext(base);
}
}
3.Register the listener,and unregister when unused:
onInputMethodListener = new OnInputMethodListener() {
@Override
public void onShow(boolean result) {
Toast.makeText(MainActivity.this, "Show input method! " + result, Toast.LENGTH_SHORT).show();
}
@Override
public void onHide(boolean result) {
Toast.makeText(MainActivity.this, "Hide input method! " + result, Toast.LENGTH_SHORT).show();
}
};
InputMethodHolder.registerListener(onInputMethodListener);
@Override
protected void onDestroy() {
super.onDestroy();
InputMethodHolder.unregisterListener(onInputMethodListener);
}
4.Don't forget to release when exiting(avoid memory leaks):
InputMethodHolder.release();
Please read Sample for getting specific use, and if have any problems please submit ISSUE.
onShow
works well in most situations, but onHide
can only give callbacks by calling hideSoftInputFromWindows
manually.
The reason is that system keyboard is hold by another process,
and the procedure for using the keyboard by an application is a local process remote call through InputMethodManager
,
the hook is just that InputMethodManager of the application's process.
public interface OnInputMethodListener {
void onShow(boolean result);
void onHide(boolean result);
}
Copy right 2017. Linmin qiu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.