我正在创建AccesibilityService,它需要在其他应用程序中获得选定的文本。
用于服务的xml资源如下所示:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewTextSelectionChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:settingsActivity="com.example.android.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();
}这应该得到文本从任何其他应用程序后,选择发生。
它只在我的应用程序中工作,我使用TextView进行活动,其中包含随机文本,在我选择它之后,就会正确地抛出事件。但是,在输入其他应用程序之后,例如chrome,一些随机的pdf阅读器应用程序,就不会抛出任何事件,至少不是我正在收听的这个应用程序。
@Edit从xml中删除了这一行。事件,但选择的索引为-1和-1。
android:packageNames="com.example.jakubjab.getselectedtext"发布于 2017-12-11 03:21:11
至少您的XML配置是关闭的:
android:packageNames="com.example.jakubjab.getselectedtext"你得省略这一行。packageNames参数限制从其中获取事件的包。不设置从每个包中获取事件的结果。
考虑到你对这个问题的解释,我想这会解决你的问题。至少,这是一个问题,如果它不能正确解决,您可能需要提供更多的细节。
https://stackoverflow.com/questions/47742745
复制相似问题