How do I detect if software keyboard is visible on Android Device or not?

前端 未结 30 1385
情书的邮戳
情书的邮戳 2020-11-22 10:59

Is there a way in Android to detect if the software (a.k.a. \"soft\") keyboard is visible on screen?

30条回答
  •  孤街浪徒
    2020-11-22 11:53

    In Android you can detect through ADB shell. I wrote and use this method:

    {
            JSch jsch = new JSch();
            try {
                Session session = jsch.getSession("", "", 22);
                session.setPassword("");
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                session.connect();
    
                ChannelExec channel = (ChannelExec)session.openChannel("exec");
                BufferedReader in = new BufferedReader(new    
                InputStreamReader(channel.getInputStream()));
                channel.setCommand("C:/Android/android-sdk/platform-tools/adb shell dumpsys window 
                InputMethod | findstr \"mHasSurface\"");
                channel.connect();
    
                String msg = null;
                String msg2 = " mHasSurface=true";
    
                while ((msg = in.readLine()) != null) {
                    Boolean isContain = msg.contains(msg2);
                    log.info(isContain);
                    if (isContain){
                        log.info("Hiding keyboard...");
                        driver.hideKeyboard();
                    }
                    else {
                        log.info("No need to hide keyboard.");
                    }
                }
    
                channel.disconnect();
                session.disconnect();
    
            } catch (JSchException | IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题