我在应用程序中使用Accessibility服务,并在onEvent方法中获取ANR,同时调用event.getSource();
当我把这段代码放在另一个线程中时,为了解决ANR上面的问题,它创建了一个异常:
java.lang.IllegalStateException:无法对未密封的示例执行此操作。
请指导我如何解决?代码如下:
@Override
public void onAccessibilityEvent(final AccessibilityEvent event) {
int eventType = event.getEventType();
if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
OnEvent(this, event);
}
}
private void OnEvent(final Context context, AccessibilityEvent event) {
String eventText = "", eventPackageName = "", eventClassName = "";
try {
if (event.getText() != null && event.getText().size() > 0)
eventText = event.getText().toString();
if (event.getPackageName() != null)
eventPackageName = event.getPackageName().toString();
if (event.getClassName() != null)
eventClassName = event.getClassName().toString();
AccessibilityNodeInfo eventSource = event.getSource();
final AccessibilityEventModel accessibilityEventModel = new AccessibilityEventModel();
accessibilityEventModel.setPackageName(eventPackageName);
accessibilityEventModel.setEventText(eventText);
accessibilityEventModel.setEventClassName(eventClassName);
accessibilityEventModel.setEventSource(eventSource);
accessibilityEventModel.setParcelable(event.getParcelableData());
executorService.execute(() -> {
AccessibilityManager accessibilityManager = new AccessibilityManager(context, localDatabaseSource);
accessibilityManager.goThroughAccessibilityEvent(eventType,
accessibilityEventModel);
});
} catch (Exception e) {
Log.d(TAG, " Error: " + e.getMessage());
}
}
fun goThroughAccessibilityEvent(
eventType: AccessibilityEventType,
accessibilityEventModel: AccessibilityEventModel
) {
when (eventType) {
AccessibilityEventType.TYPE_WINDOW_CONTENT_CHANGED -> WindowContentChangeEventData(
localDatabaseSource
).onAccessibilityEvent(context, accessibilityEventModel)
}
}
1条答案
按热度按时间iklwldmw1#
我不知道你在生成
accessibilityEventModel
之后实际上在做什么,但你应该尝试将整个try/catch
(以及任何其他需要使用accessibilityEventModel
的逻辑)块放在后台线程中: