Vis*_*u V 5 android accessibilityservice
我正在尝试使用辅助功能服务来检测键盘是否处于活动状态(我的应用程序之外).为此,我试图阅读通知"选择键盘"(当启用多个键盘时).使用以下代码.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在除了"键盘选择器"通知之外,应用程序记录所有通知.如何阅读该通知,是可能的.
谢谢
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");
}
}
Run Code Online (Sandbox Code Playgroud)
在清单中:添加
<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>
Run Code Online (Sandbox Code Playgroud)
安装应用程序后允许阅读设置中的通知。已在 Android 5.0 上测试并运行
| 归档时间: |
|
| 查看次数: |
1103 次 |
| 最近记录: |