Giter Site home page Giter Site logo

pqpo / inputmethodholder Goto Github PK

View Code? Open in Web Editor NEW
428.0 12.0 57.0 118 KB

A keyboard listener for Android which by hooking the InputMethodManager. 通过hook监听系统键盘显示

Home Page: https://pqpo.me/

License: Apache License 2.0

Java 100.00%
hook inputmethod

inputmethodholder's Introduction

InputMethodHolder

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 using ServiceManagerHook to hook ServiceManager, learn more from InputMethodManagerHook. If you like this project, ,welcome to fork or star!

Steps:

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.

Defect

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);
}

Sample

Contact

[email protected]

License

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.

inputmethodholder's People

Contributors

pqpo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

inputmethodholder's Issues

监听键盘收起

我发现输入法启动的时候,会回调IInputMethodManagerService中的windowGainedFocus该方法,这个方法中会创建EditText和InputMethodService的通道,即IInputConnectionWrapper,在创建这个Binder的过程中,需要参数InputConnection对象,而这里是可以hook住的。这样键盘收起的时候就可以监听到了。测试6.0可以检测到,不知道对于完善你的Hook是否有帮助,麻烦了。

关于Hook住EditText中InputConnection的问题

虚拟键盘在更新输入框文字的时候是通过InputConnection这个最后来实现的,为什么Hook不住EditText中的InputConnection呢?
实际效果是Hook完毕后,点击虚拟键盘上面的文字,并没有回调InputConnection中相应的方法

在android 4.4上不生效

06-19 10:25:56.721 12910-12910/pw.qlm.softinputmethodlistener W/InputMethodHolder: hook failed! detail:java.lang.ClassNotFoundException: android.app.SystemServiceRegistry at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:251) at java.lang.Class.forName(Class.java:216) at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.Class(SystemServiceRegistryCompat.java:17) at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.getSystemFetcher(SystemServiceRegistryCompat.java:23) at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.clearCachedService(InputMethodManagerHook.java:70) at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.onHook(InputMethodManagerHook.java:60) at pw.qlm.inputmethodholder.InputMethodHolder.init(InputMethodHolder.java:41) at pw.qlm.sample.MyApplication.attachBaseContext(MyApplication.java:18) at android.app.Application.attach(Application.java:181) at android.app.Instrumentation.newApplication(Instrumentation.java:991) at android.app.Instrumentation.newApplication(Instrumentation.java:975) at android.app.LoadedApk.makeApplication(LoadedApk.java:502) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301) at android.app.ActivityThread.access$1500(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NoClassDefFoundError: android/app/SystemServiceRegistry at java.lang.Class.classForName(Native Method)  at java.lang.Class.forName(Class.java:251)  at java.lang.Class.forName(Class.java:216)  at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.Class(SystemServiceRegistryCompat.java:17)  at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.getSystemFetcher(SystemServiceRegistryCompat.java:23)  at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.clearCachedService(InputMethodManagerHook.java:70)  at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.onHook(InputMethodManagerHook.java:60)  at pw.qlm.inputmethodholder.InputMethodHolder.init(InputMethodHolder.java:41)  at pw.qlm.sample.MyApplication.attachBaseContext(MyApplication.java:18)  at android.app.Application.attach(Application.java:181)  at android.app.Instrumentation.newApplication(Instrumentation.java:991)  at android.app.Instrumentation.newApplication(Instrumentation.java:975)  at android.app.LoadedApk.makeApplication(LoadedApk.java:502)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)  at android.app.ActivityThread.access$1500(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5001)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.ClassNotFoundException: Didn't find class "android.app.SystemServiceRegistry" on path: DexPathList[[zip file "/data/app/pw.qlm.softinputmethodlistener-1.apk"],nativeLibraryDirectories=[/data/app-lib/pw.qlm.softinputmethodlistener-1, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at java.lang.Class.classForName(Native Method)  at java.lang.Class.forName(Class.java:251)  at java.lang.Class.forName(Class.java:216)  at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.Class(SystemServiceRegistryCompat.java:17)  at pw.qlm.inputmethodholder.compat.SystemServiceRegistryCompat.getSystemFetcher(SystemServiceRegistryCompat.java:23)  at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.clearCachedService(InputMethodManagerHook.java:70)  at pw.qlm.inputmethodholder.hook.InputMethodManagerHook.onHook(InputMethodManagerHook.java:60)  at pw.qlm.inputmethodholder.InputMethodHolder.init(InputMethodHolder.java:41)  at pw.qlm.sample.MyApplication.attachBaseContext(MyApplication.java:18)  at android.app.Application.attach(Application.java:181)  at android.app.Instrumentation.newApplication(Instrumentation.java:991)  at android.app.Instrumentation.newApplication(Instrumentation.java:975)  at android.app.LoadedApk.makeApplication(LoadedApk.java:502)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)  at android.app.ActivityThread.access$1500(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5001)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  at dalvik.system.NativeStart.main(Native Method)  06-19 10:25:56.731 12910-1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.