我正在尝试使用辅助功能服务检测键盘是否处于活动状态(我的应用程序之外)。为此,我尝试阅读通知"选择键盘" (启用多个键盘时)。使用以下代码。
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;
}
}
现在所有通知都由应用程序记录,除了键盘选择器&#34;通知。如何阅读该通知是可能的。
谢谢
答案 0 :(得分:2)
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:添加
<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>
安装应用程序后,允许在设置中读取通知。 在Android 5.0上测试并使用
答案 1 :(得分:1)
如果我是正确的,无法获得该通知,系统本身会检测所有可用的键盘并显示它们(如果只有一个键盘,则不会弹出)。
据我所知,没有办法收到通知。