title: How to interact with USSD dialog programmatically in android
body: <p>I want to use <strong>USSD dialog</strong> which comes after dialing any <strong>USSD code</strong> say <strong>*123#</strong> which asks user to enter option number to perform specific task(s) depending upon sim card vendors. I need to interact with that dialog to provide input in the text box given into it programmatically.</p>
  
  <p>However, I am able to read the <strong>USSD response</strong> that comes in Alert Dialog after dialing any USSD code, using <code>AccessibilityService</code> and I'm showing the response in a <code>Toast</code> as shown in the code below. I haven't found any solution to interact with USSD dialog yet.</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Here is the service declaration in Manifest:</p>
  
  <pre><code>&lt;service android:name=".UssdService"
      android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.accessibilityservice.AccessibilityService" /&gt;
      &lt;/intent-filter&gt;
      &lt;meta-data android:name="android.accessibilityservice"
          android:resource="@xml/config_service" /&gt;
  &lt;/service&gt;
  </code></pre>