How to programmatically interact with USSD dialog box in Android
I want to use the USSD dialog box, which will appear after dialing any USSD code of * 123#. The dialog box requires the user to enter the option number to perform a specific task, depending on the SIM card supplier. I need to interact with the dialog box to provide input in the dialog box programmatically
However, I can use accessibilityservice to read the USSD response in the alarm dialog box after dialing any USSD code, and display the response in toast, as shown in the following code. I have not found any solution to interact with the USSD dialog box
public class UssdService extends AccessibilityService{
public static String TAG = "USSD";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG, "onAccessibilityEvent");
String text = event.getText().toString();
if (event.getClassName().equals("android.app.AlertDialog")) {
Log.d(TAG, text);
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInterrupt() {
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.flags = AccessibilityServiceInfo.DEFAULT;
info.packageNames = new String[]{"com.android.phone"};
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
info.FeedbackType = AccessibilityServiceInfo.FeedBACK_GENERIC;
setServiceInfo(info);
}
}
This is the service declaration in the list:
<service android:name=".UssdService"
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/config_service" />
</service>
resolvent:
To interact with the USSD dialog box, I use the following code
I use the following code for the click event:
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("Send");
for (AccessibilityNodeInfo node : list) {
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
I use the following code for settext in EditText. This is the settext where the current focus is
AccessibilityNodeInfo nodeInput = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
Bundle bundle = new Bundle();
bundle.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,pMPIN);
nodeInput.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,bundle);
nodeInput.refresh();