在Service中全局监听输入法意思就是可以在任何界面都可以监听输入法,不局限于自己的APP。
方法一:根据切换输入法会在通知栏显示通知实现
- 优点:简单高效
- 缺点:手机必须安装两个以上输入法
通常语言为中文的情况下切换输入法的通知栏标题有”选择输入法”,”更改键盘”,”选择键盘”,”更改输入法”这四种,在NotificationListenerService
onNotificationPosted(StatusBarNotification sbn)
onNotificationRemoved(StatusBarNotification sbn)
两个方法中分别检测该四个关键字就可以实现打开关闭输入法的监听。
public static List<String> sKeyboardTitleList = new ArrayList<>(Arrays.asList(new String[] {
"选择输入法","更改键盘","选择键盘","更改输入法"}));
public void onNotificationPosted(StatusBarNotification sbn) {
String title = sbn.getNotification().extras.getString("android.title");
if (title == null) return;
if(sKeyboardTitleList.contains(title)){
//输入法打开
}
}
public void onNotificationRemoved(Sta