Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android, how to detect a system dialog is displayed (power options, recent apps, low battery...)?

I'd like to catch anything that causes the display of my Activity to get even partially hidden, e.g. power options, recent apps tray, low battery notification, etc... and I'm having a hard time to detect these system events.

I was pretty sure onPause() would be called when such events happen, but it seems to be wrong... or is it me?

Any other idea?... I'd preferably not hook on each system broadcast action individually, since I'd like to be as generic as possible (and react to ANYTHING that hides my Activity).

like image 792
Wouzz Avatar asked Jan 20 '12 13:01

Wouzz


1 Answers

On working on a kiosk style app I know that some Dialogs come to the foreground and can be detected by

    ActivityManager activityManager = (ActivityManager)getBaseContext()
       .getSystemService(Activity.ACTIVITY_SERVICE);
    String className = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();

An example for that is the bluetooth-binding dialog that brings the com.android.settings to the foreground.

A counter-example is the power-button dialog (Turn off, Reboot etc) that does not come to the foreground.

Note that you can close system dialogs (even the power-button dialog) with this broadcast:

    Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);

But on most (all newer?) devices this Broadcast will even close the software keyboard, so it is not advisable to have a service running that frequently sends it as the user will then be unable to enter anything into a text field.

Note that such behaviour will definetly gratify your app a status as beeing malware, keeping it from beeing published on google play.

like image 135
FrankKrumnow Avatar answered Nov 15 '22 07:11

FrankKrumnow