TAGS :Viewed: 13 - Published at: a few seconds ago

[ Start service when keyboard is visible - Android ]

I saw many posts about Keyboard visibility in Android. BUT, I need to start one Service when any application opens keyboard (Messaging, Viber, WhatsApp etc.), and as I understood other posts, it can be detected only if keyboard was started by my app, not by any other app.

How can I achieve starting service if ANY application started keyboard?

I hope you understand me. Please, have patience because I just started programming in Android.


I need to be able to do this without root access, or with some complicated User's actions. This would use many users so it needs to be flexible and simple (on their side).

Answer 1

Since you'll be accounting for all apps, you'll have to reach into system resources to get the state.

You'll have to open a shell in your program via exec(), and run this command: dumpsys window InputMethod | grep "mHasSurface", pulled from this (very underrated) answer: Shell command for keyboard state. He also includes an interesting link on how to authorize the command for your app on non-rooted devices.

Refer to this S/O on different ways to run an exec(): Android Java execute shell. If you have root, just copy what they have. If not, change their code to be exec() instead of exec('su'), and either a) try the hack in the OP's answer, or b) sign your app with the system key.

Hope this helps!