有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AccessibilityService获取所选文本

我正在创建AccessibilityService,它需要在其他应用程序中获取选定的文本

我的xml服务资源如下所示:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:accessibilityEventTypes="typeViewTextSelectionChanged"
    安卓:accessibilityFeedbackType="feedbackSpoken"
    安卓:canRetrieveWindowContent="true"
    安卓:notificationTimeout="100"
    安卓:settingsActivity="com.example.安卓.apis.accessibility.TestBackActivity"/>

和java代码

@Override
public void onServiceConnected(){

    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
    info.notificationTimeout = 100;
    this.setServiceInfo(info);
    Toast.makeText(this,"Connected",Toast.LENGTH_LONG).show();
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event)
{
    Toast.makeText(this,"Got event",Toast.LENGTH_LONG).show();

    final int selectBegin = event.getSource().getTextSelectionStart();
    final int selectEnd = event.getSource().getTextSelectionEnd();
    if (selectBegin == selectEnd)
    {
        return;
    }
    String text = event.getText().toString().substring(selectBegin,selectEnd + 1);
the rest of code...
}

@Override
public void onInterrupt()
{
    return;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(this,"Stopped",Toast.LENGTH_LONG).show();
}

选择发生后,该应用程序将从任何其他应用程序获取文本

它只在我的应用程序中起作用,我用文本视图进行活动,其中包含随机文本,选择后,事件被正确抛出。但在进入任何其他应用程序后,例如chrome,一些随机的pdf阅读器应用程序,都不会抛出任何事件,至少我正在收听的这一个不会

@Edit 从xml中删除了这一行。现在抛出事件,但选择的索引为-1和-1

安卓:packageNames="com.example.jakubjab.getselectedtext"

共 (0) 个答案