我已经使用AccessibilityService
完成了这项工作。它只会在API级别> = 16时正常工作。您需要延长AccessibilityService
。例如,这个类将获得USSD响应文本并关闭对话框。
// ....
public class UssdAccessibilityService extends AccessibilityService {
public UssdAccessibilityService() {
}
@TargetApi(16)
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (!"com.android.phone".equalsIgnoreCase((String)event.getPackageName())){
// In this example we are only interested in events comming
// from "com.android.phone" package
event.recycle();
return;
}
String className = (String)event.getClassName();
if (className == null || (!className.contains("AlertDialog") && !className.contains("AlertDialog"))){
// Class is not an USSD dialog
event.recycle();
return;
}
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
// getSource() is annotated @Nullable, so we do this to be
// safe just in case
event.recycle();
return;
}
AccessibilityNodeInfo acceptButton = null;
String ussdText = null;
int childCount = source.getChildCount();
for (int i = 0; i < childCount; i++){
AccessibilityNodeInfo current = source.getChild(i);
if (current == null)
continue;
String currentText = (String)current.getText();
if (current.isClickable()){
// In the case of USSD dialogs, there is only one clickable.
// May be necessary to do more robust search in other scenarios
acceptButton = current;
continue;
}
ussdText = currentText;
current.recycle();
}
if (ussdText!= null) {
if (acceptButton != null)
acceptButton.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
source.recycle();
event.recycle();
}
// ....
}
您必须<application>
<service
android:name=".UssdAccessibilityService"
android:enabled="true"
android:label="Read USSD codes and dismiss"
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/accessibility_service_config" />
</service>
下申报清单中的辅助服务在res/xml
创建accessibility_service_config.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:packageNames="com.android.phone,com.ats.android.activationcodebot"
android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>
当然你有这个代码适应自己的需要。
最后,您必须在Android上手动启用无障碍服务设置>辅助功能(或要求用户这样做)。
阅读全文...Developing an Accessibility Service
你想点击文字或使文字的一部分可点击,或获得屏幕点击坐标吗?有关前者的信息可以在此处获得:https://www.google.com.ng/url?url=https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the -text - 视图 - 是 - 点击&RCT = J&SA = U&VED = 0ahUKEwilotjX6-7UAhXIYlAKHfF8BwgQFggbMAA&q =的Android +让+ +文字+的一部分+点击与USG = AFQjCNEVpYcyHrHbO5NKt5jBPVqAjpNiFg –
@财经部,hendyEjegi我想点击的文本,而无需自己做的用户。但由于它在另一个应用程序上,我无法点击它,这正是我想要解决的问题。 – Jayce
检查辅助功能api,所有我可以帮助更多的信息阅读https://stackoverflow.com/questions/35842762/how-to-read-window-content-using-accessibilityservice-and-evoking-ui-using-dra – ingsaurabh