我正在尝试使用Accessibility Service
从Windows屏幕获取所有文本。一切正常,但是当我快速输入EditText
时,应用程序和键盘会在几秒钟内冻结。这很奇怪,因为它只发生在我应用程序中的所有EditText
上,而其他应用程序则没有。
这是我的代码。我捕获了每个事件,并使用递归调用在AccessibilityNodeInfo
中深入了解文本。谢谢!
public class WebAccessibilityService extends AccessibilityService {
....
@Override
public void onAccessibilityEvent(final AccessibilityEvent event) {
CharSequence eventPackageName = event.getPackageName();
if (eventPackageName != null) {
if (eventPackageName.equals(BuildConfig.APPLICATION_ID)) return;
int eventType = event.getEventType();
if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED || eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
AccessibilityNodeInfo mNodeInfo = event.getSource();
String value = getAllTextViews(mNodeInfo); //When I remove this line, app does not freeze anymore.
}
}
}
private String getAllTextViews(AccessibilityNodeInfo nodeInfo){
if (nodeInfo == null) return "";
StringBuilder val = new StringBuilder();
if (!TextUtils.isEmpty(nodeInfo.getText())) {
val.append(nodeInfo.getText());
}
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
String v = getAllTextViews(nodeInfo.getChild(i));
if (!TextUtils.isEmpty(v))
val.append(v).append(" ");
}
return val.toString().replace('\n', ' ').replaceAll(" +", " ");
}
}
还有日志猫
09-01 16:09:22.197 30797-30797/com.androidapp.watchme D/detect===: EventType=TYPE_WINDOW_CONTENT_CHANGED, package=com.sec.android.inputmethod
09-01 16:09:22.207 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 0
09-01 16:09:22.236 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 1
09-01 16:09:22.239 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 0
09-01 16:09:22.249 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 1
09-01 16:09:22.251 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 0
09-01 16:09:22.256 30797-30797/com.androidapp.watchme D/ViewRootImpl@aa32041[SettingsActivity]: ViewPostIme pointer 1
09-01 16:09:22.262 30797-30797/com.androidapp.watchme D/detect===: EventType=TYPE_WINDOW_CONTENT_CHANGED, package=com.sec.android.inputmethod
09-01 16:09:22.265 30797-30797/com.androidapp.watchme I/Choreographer: Skipped 303 frames! The application may be doing too much work on its main thread.
09-01 16:09:22.274 30797-30797/com.androidapp.watchme W/IInputConnectionWrapper: Got RemoteException calling setTextBeforeCursor
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:761)
at com.android.internal.view.IInputContextCallback$Stub$Proxy.setTextBeforeCursor(IInputContextCallback.java:169)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:266)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
09-01 16:09:22.280 30797-30797/com.androidapp.watchme V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@f20b6c9 nm : com.androidapp.watchme ic=com.android.internal.widget.EditableInputConnection@11541ce
09-01 16:09:22.287 30797-30797/com.androidapp.watchme W/InputMethodManager: IME died: com.sec.android.inputmethod/.SamsungKeypad
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:761)
at com.android.internal.view.IInputMethodSession$Stub$Proxy.updateSelection(IInputMethodSession.java:228)
at android.view.inputmethod.InputMethodManager.updateSelection(InputMethodManager.java:2190)
at android.widget.Editor.sendUpdateSelection(Editor.java:1705)
at android.widget.Editor.finishBatchEdit(Editor.java:1550)
at android.widget.Editor.endBatchEdit(Editor.java:1524)
at android.widget.TextView.endBatchEdit(TextView.java:8799)
at com.android.internal.widget.EditableInputConnection.endBatchEdit(EditableInputConnection.java:78)
at android.view.inputmethod.BaseInputConnection.finishComposingText(BaseInputConnection.java:444)
at android.view.inputmethod.BaseInputConnection.closeConnection(BaseInputConnection.java:163)
at com.android.internal.widget.EditableInputConnection.closeConnection(EditableInputConnection.java:88)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:541)
at com.android.internal.view.IInputConnectionWrapper.dispatchMessage(IInputConnectionWrapper.java:225)
at com.android.internal.view.IInputConnectionWrapper.closeConnection(IInputConnectionWrapper.java:211)
at android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper.deactivate(InputMethodManager.java:644)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1725)
at android.view.inputmethod.InputMethodManager$H.handleMessage(InputMethodManager.java:566)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
09-01 16:09:22.288 30797-30797/com.androidapp.watchme I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
答案 0 :(得分:0)
说实话,我注意到的第一件事是这是三星设备。
09-01 16:09:22.287 30797-30797 / com.androidapp.watchme W / InputMethodManager:IME已死:com.sec.android.inputmethod / .SamsungKeypad
您是否尝试过在Pixel或Nexus设备上复制此行为?