如何使用无障碍服务阅读通知
How to read Notifications using Accessibility Services
我正在尝试获取传入消息并打印它们。在一些文章的帮助下,我设法打印了,但是有了这个,我只能得到通知标题。
我是 android 开发的新手,不知道是否可以通过 accessibilityservice 获得实际消息 body。
清单 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.accessibilitydemo">
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".AccessibilityActivity"
android:enabled="true"
android:exported="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/serviceconfig" />
</service>
</application>
</manifest>
Serviceconfig.xml :
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:notificationTimeout="100"
android:packageNames="@null"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true" />
AccessibiliActivity :
package com.domain.accessibilitydemo;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;
public class AccessibilityActivity extends AccessibilityService {
String txtMessage;
@Override
public void onInterrupt() {
}
@Override
protected void onServiceConnected() {
System.out.println("onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.notificationTimeout = 100;
info.packageNames = null;
setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
//if(event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED){
if (event.getPackageName().toString().equals("com.whatsapp")){
StringBuilder message = new StringBuilder();
if (!event.getText().isEmpty()) {
for (CharSequence subText : event.getText()) {
message.append(subText);
}
txtMessage = message.toString();
MainActivity.textMsg.setText(txtMessage);
}
}
}
}
}
输出:
OUTPUT SCREENSHOT
如何访问实际消息?
有没有办法将它们存储在列表视图中?
谢谢
不同类型的事件携带不同的数据;对于这种特殊类型,您可以使用 getParcelableData() 获取通知正文。
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
Notification notification = (Notification) accessibilityEvent.getParcelableData();
if (notification == null) return;
String title = notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString();
String text = notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
String text2 = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT).toString();
String package_name = accessibilityEvent.getPackageName().toString();
break;
我正在尝试获取传入消息并打印它们。在一些文章的帮助下,我设法打印了,但是有了这个,我只能得到通知标题。 我是 android 开发的新手,不知道是否可以通过 accessibilityservice 获得实际消息 body。
清单 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.accessibilitydemo">
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".AccessibilityActivity"
android:enabled="true"
android:exported="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/serviceconfig" />
</service>
</application>
</manifest>
Serviceconfig.xml :
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:notificationTimeout="100"
android:packageNames="@null"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true" />
AccessibiliActivity :
package com.domain.accessibilitydemo;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;
public class AccessibilityActivity extends AccessibilityService {
String txtMessage;
@Override
public void onInterrupt() {
}
@Override
protected void onServiceConnected() {
System.out.println("onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.notificationTimeout = 100;
info.packageNames = null;
setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
//if(event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED){
if (event.getPackageName().toString().equals("com.whatsapp")){
StringBuilder message = new StringBuilder();
if (!event.getText().isEmpty()) {
for (CharSequence subText : event.getText()) {
message.append(subText);
}
txtMessage = message.toString();
MainActivity.textMsg.setText(txtMessage);
}
}
}
}
}
输出:
OUTPUT SCREENSHOT
如何访问实际消息? 有没有办法将它们存储在列表视图中?
谢谢
不同类型的事件携带不同的数据;对于这种特殊类型,您可以使用 getParcelableData() 获取通知正文。
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
Notification notification = (Notification) accessibilityEvent.getParcelableData();
if (notification == null) return;
String title = notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString();
String text = notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
String text2 = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT).toString();
String package_name = accessibilityEvent.getPackageName().toString();
break;