How to close/cancel/dismiss a System Dialog programmatically (Android)

All we need is a simple explanation of the problem, which is provided below.

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 they use the dialog like this:

AlertDialog newDialog = new AlertDialog.Builder(context)
                    .setPositiveButton(R.string.ok, null)

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?

Let’s Solve it:

This is a common error many developers questioned us about it. So we write the explanation above. You just have to apply the suggested solution to your code and it will do for you. If you still getting this error after applying this code then comment below we will get back to you with the new method.

Solution 1

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.

Solution 2

Just add this line in your code.

Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

Solution 3

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.

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";
    public void onAccessibilityEvent(AccessibilityEvent event) {
   //In my mobile the class name has been looks like this.
        if (event.getClassName().equals("")) {
 //Method performGlobalAction (GLOBAL_ACTION_BACK) requires Android     
  //  4.1+

    public void onInterrupt() {

protected void onServiceConnected() {
      Log.v(TAG, "onServiceConnected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.packageNames = new String[]
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;


In Manifest add the following things:

  android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<action android:name="android.accessibilityservice.AccessibilityService" />

Solution 4

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

    val closeDialog = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)

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

            <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />

Note: You are free to use these solutions for your personal use. We recommend you apply the first solution to your code because it was tested in our system before posting it on this page.

We are always trying to help the developer community, So we made their work easy. Basically, we collected these data from, As it is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0.

Leave a Comment