45

I have to dismiss this system Dialog (Attached below). I am getting this value but I am not able to dismiss it programmatically in Service not in Activity.

My question is:

  1. Is it possible to dismiss it ? if yes please help or guide me how to achieve it.

enter image description here

Ash
  • 671
  • 2
  • 7
  • 18

4 Answers4

26

Please check it

@Override
public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (! hasFocus) {
            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }

It is working in my code.

SystemParadox
  • 8,203
  • 5
  • 49
  • 57
Ash
  • 682
  • 2
  • 10
  • 20
  • I am accepted this answer, because it is working in Activity class. – Ash Jun 04 '14 at 12:29
  • 1
    Thanks @Ashutosh, but It is not working in Service class. Please provide some methods which is working in Service. – Ash Jun 04 '14 at 12:30
  • works fine in cat b15q (kitkat 4.4.2) but on samsung xcover 3 (kitkat 4.4.4 with TouchWiz) only works if the intent is broadcasted after the statusbar is pulled down, if the intent is fired while the status bar while the user is sliding the status nothing happens – rjlopes Sep 17 '15 at 11:26
  • Working. THANK YOU. – Aamir Ali Apr 29 '17 at 22:39
  • 1
    it will not work when you have another popup/dialog. – Ashish Jain Apr 17 '19 at 11:04
  • @Ash Will this close even the permission prompts ? – Vasanth Sep 10 '20 at 05:39
22

You can use - ACTION_CLOSE_SYSTEM_DIALOGS

Broadcast Action: This is broadcast when a user action should request a temporary system dialog to dismiss.

public static final String ACTION_CLOSE_SYSTEM_DIALOGS

Added in API level 1

Broadcast Action: This is broadcast when a user action should request a temporary system dialog to dismiss. Some examples of temporary system dialogs are the notification window-shade and the recent tasks dialog.

Constant Value: "android.intent.action.CLOSE_SYSTEM_DIALOGS"

This information can be found at android developer site.

Working example-

Android Manifest-

<receiver android:name=".SystemDialogReceiver">
            <intent-filter>
                <action android:name="android.intent.
                 action.CLOSE_SYSTEM_DIALOGS" />
            </intent-filter>
</receiver>

Class file-

class SystemDialogReceiver extends BroadcastReceiver {
        private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
        private static final String 
        SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
                String dialogType = intent.
                getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if(dialogType != null && dialogType.
                equals(SYSTEM_DIALOG_REASON_RECENT_APPS)){
                    Intent closeDialog = 
                    new Intent(Intent.
                    ACTION_CLOSE_SYSTEM_DIALOGS);
                    context.sendBroadcast(closeDialog);

                }
            }

        }

    }
sjain
  • 23,126
  • 28
  • 107
  • 185
  • would you elaborate your answer because it seems to be work but problem is it prevents to open all system dialog outside the application, Please explain detail – Ash Jun 03 '14 at 13:12
  • This is what the intent `ACTION_CLOSE_SYSTEM_DIALOGS` does on "user's request". It closes any system dialog when appear. Check the official docs. – sjain Jun 03 '14 at 13:41
  • But I have to dismiss only system dialog which is generated from application. IF I want to check balance then dialog should work and cancel after click event. Please help how to handle it – Ash Jun 03 '14 at 14:25
  • I am trying to sendBroadcast to close system close system dialogs, val closeDialog = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) context!!.sendBroadcast(closeDialog) from within a broadcast receiver, when ACTION_SCREEN_OFF or ACTION_SCREEN_ON is detected , for some reason it does not work. I am getting the action in onReceive of the BroadcastReceiver. Do you have any idea why that might be happening ? – Anubhav May 12 '21 at 18:23
  • Please note ACTION_CLOSE_SYSTEM_DIALOGS is going to be removed on Android-12 (S). https://developer.android.com/reference/android/content/Intent#ACTION_CLOSE_SYSTEM_DIALOGS – KenIchi May 27 '21 at 07:56
13

Try using the following:

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • just a followup question. Will it be able to dismiss all system dialog? even the battery critically low? are there any exceptions to this? – SMR Jun 03 '14 at 11:24
  • 3
    @Nermeen I have already used it, Intent closeDialog = newIntent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); sendBroadcast(closeDialog); but this is not working – Ash Jun 03 '14 at 11:29
3

You can use ACTION_CLOSE_SYSTEM_DIALOGS when window focus is lost. But please note that doing so will also prevent user from turning off the phone while your app is running because the Power Off dialog is also a system dialog.

user3063925
  • 255
  • 3
  • 12