Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect keyboard is active outside the application on android

I am trying to detect keyboard is active or not (Outside my application) using accessibility service. For that i tried to read the notifications "choose keyboard" (When multiple key board are enabled). 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 logged by the application except "keyboard chooser" notification. How to read that notification, is that possible.

Thanks

like image 912
Vishnu V Avatar asked Oct 19 '22 20:10

Vishnu V


1 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 : 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 application allow reading notification in settings. Tested and working on Android 5.0

like image 121
Satty Avatar answered Oct 22 '22 10:10

Satty