9

I have an app that make USSD calls, but after all USSD calls i got a dialog with the result to the user.

I know that is possible to dismiss this dialog because the "USSD Checker" app do this, they get the response from USSD without showing the user dialog.

In the phone utils class

Has the function displayMMIComplete that after complete the Ussd call, show a TYPE_SYSTEM_DIALOG. In the PhoneUtils.java they use the dialog like this:

AlertDialog newDialog = new AlertDialog.Builder(context)
                    .setMessage(text)
                    .setPositiveButton(R.string.ok, null)
                    .setCancelable(true)
                    .create();
newDialog.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
newDialog.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_DIM_BEHIND);
newDialog.show();

Then i can't just send some flag to dismiss it. And I can't use that import its a system class.

And when I do X consecutive calls appears X dialogs to the user close, and my app will need to do consecutive calls, there is anyway to programmatically close this System dialog?

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Nilson Aguiar
  • 144
  • 1
  • 2
  • 11
  • By the way, this code doesn't work for me even if I add `android.permission.SYSTEM_ALERT_WINDOW` to the AndroidManifest.xml. `android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRoot$W@40527640 -- permission denied for this window type` – Maksim Dmitriev Sep 27 '13 at 12:23
  • http://stackoverflow.com/q/4344523/1065835. It works – Maksim Dmitriev Sep 27 '13 at 12:51

4 Answers4

6

I found an Answer to my question if you're trying to use USSD Calls you can disable the result text

In a russian blog there is a post that show how you can connect to the phoneutils service of android, then control the texts from USSD (CALL and RESULT). He show a sample using a interface (IExtentedendNetworkService ) that binds on phone utils just on the android OS start and if there was not any other app trying to do the same (Because just one service can be bound and maybe will be your or not, I don't know the rule that Android OS uses to choose).

In the function "CharSequence getUserMessage(CharSequence text);" if you return null the result dialog will not appear.

Nilson Aguiar
  • 144
  • 1
  • 2
  • 11
5

Just add this line in your code.

Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(intent);
droidd
  • 1,361
  • 10
  • 15
2

I have found a simple answer from this blog here. First we have to create a simple Accessibility service.The Accessibility Service works only if it's enable.In setting there is a option Accessibility,in that turn on your project and give permission to your service class.

Note: Method performGlobalAction (GLOBAL_ACTION_BACK) requires Android 4.1+, if it is not used, it is possible to meet the 4.0. It closes the window immediately after the AlertDialog,

here the sample code:

 public class USSDService extends AccessibilityService {
 String TAG="USSDService";
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
   //In my mobile the class name has been looks like this.
        if (event.getClassName().equals("com.mediatek.phone.UssdAlertActivity")) {
 //Method performGlobalAction (GLOBAL_ACTION_BACK) requires Android     
  //  4.1+
            performGlobalAction(GLOBAL_ACTION_BACK);
        }
    }

    @Override
    public void onInterrupt() {


    }
@Override
protected void onServiceConnected() {
    super.onServiceConnected();
      Log.v(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);
 }

}

In Manifest add the following things:

<service 
  android:name=".USSDService"
  android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35
  • doesnt work for me either following this https://umeshisran4android.blogspot.co.id/2015/11/how-to-readparse-ussd-messages.html not works at all, the ussd dialog still there waiting for input – dmh Jun 02 '16 at 03:36
  • Setting->Accessibility Setting -> You can see a option 'your app name'. Turn it on. – krishnan muthiah pillai Jun 02 '16 at 09:57
  • i can get the log, from USSD but the USSD Dialog itself won't close unless i click cancel close it – dmh Jun 03 '16 at 23:16
  • Hello. Thanks. It works for me. BUT I can't get the text content of the ussd dialog response... I only get `[PHONE]` as `mtext` instead of the actual text...Could you get the content of the ussd dialog before or after closing it with the code above ? – Jason Krs Jan 31 '18 at 19:35
  • @JasonKrs you need to check this [link](https://stackoverflow.com/questions/22057625/prevent-ussd-dialog-and-read-ussd-response) for getting the ussd dialog response. – krishnan muthiah pillai Feb 14 '18 at 13:11
0

You can send a system level broadcast to dismiss all system dialogs

    val closeDialog = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
    sendBroadcast(closeDialog)

and add the intent filter in your Service or BroadcastReceiver class in the manifest file.

    <intent-filter>
            
            <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
 
    </intent-filter>
Anubhav
  • 1,984
  • 22
  • 17