0

I want to dismiss a system generated alert dialog programmatically. I have tried all solutions provided here(stackoverflow) but it does not seem to work. This is the accepted answer mostly everywhere, but it only dismisses notification panel and recent tasks menu.

I have tested it on devices with os version 4.0.3, 4.2.2, 4.4.2 and 5.1.1, it has the same behavior on all of them. There are apps which can actually dismiss all system dialogs (Mubble). Can someone suggest how it is done?

Thanks

Community
  • 1
  • 1
Ishaan
  • 3,658
  • 2
  • 23
  • 39
  • Are you sure your broadcast to close intents is getting fired? Place a broadcast receiver for the same, and check. – shiladitya Dec 22 '15 at 19:37
  • @shiladitya: Yes, the broadcast is getting fired. The power off dialog, recent tasks menu and notification panel gets dismissed; however the system alert dialog that I want to get dismissed is not going away. – Ishaan Dec 22 '15 at 19:52
  • I have tried exactly http://stackoverflow.com/a/24013884/3024565 but it does not help either. :( – Ishaan Dec 23 '15 at 03:55

1 Answers1

-1

The usual answer to this is

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

However, this does not work for everything. In particular, on some devices it does not work for the "recent apps" list.

The solution is to detect when your app loses focus and then move your app to the front. Note that your app will need the permission android.permission.REORDER_TASKS to do this.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        this.keepFocus = true;
    }
    if (! hasFocus && this.keepFocus) {
        ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_WITH_HOME );
    }
}
SystemParadox
  • 8,203
  • 5
  • 49
  • 57