Keyboard Detection Actively Outside Android Application

I am trying to detect if the keyboard is active or not (outside my application) using the accessibility service. To do this, I tried to read the β€œselect keyboard” notifications (if multiple keyboard is enabled). The following code is used.

public class KeyboardWatcher extends AccessibilityService { boolean isConnected = false; @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) { final String packagename = String.valueOf(event.getPackageName()); Log.d("Package", packagename); String msg = ""; List<CharSequence> s = event.getText(); if(s.iterator().hasNext()) { msg += s.iterator().next().toString(); Log.d("MSG", msg); }else{ Log.d("TYPE", event.getEventType()+""); } }else{ Log.d("EVENT TYPE__",event.getEventType()+""); final String packagename = String.valueOf(event.getPackageName()); Log.d("PNE", packagename); } } protected void onServiceConnected() { if (isConnected) { return; } AccessibilityServiceInfo info = new AccessibilityServiceInfo(); info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK; setServiceInfo(info); isConnected = true; } } 

Now all notifications are registered by the application, except for the "Select Keyboard" notification. How to read this notice is possible.

thanks

+5
source share
2 answers
 package com.sat.app.notification; import android.content.Context; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.util.Log; public class NotificationService extends NotificationListenerService { Context context; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } @Override public void onNotificationPosted(StatusBarNotification sbn) { String pack = sbn.getPackageName(); String title = sbn.getNotification().extras.getString("android.title");// It will be select keyboard } @Override public void onNotificationRemoved(StatusBarNotification sbn) { Log.i("com.sat","Notification Removed"); } } 

In manifest manifest: add

 <service android:name="com.sat.app.notification.NotificationService" android:label="@string/app_name" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> 

After installing the application, allow reading notifications in the settings. Tested and works on Android 5.0

+2
source

If I'm right, we have no way to get this notification, the system itself detects all available keyboards and displays them (if there is only one keyboard, there will be no pop).
As far as I know, there is no way to get this notification.

+1
source

Source: https://habr.com/ru/post/1208990/


All Articles