我已經使用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