Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if any system dialog is displayed?

How to check if any system dialog (like the one below or USSD) is displayed in Android ?

airplane mode switching dialog

Programmatic way or cmd root way? Any variants.

like image 415
ilw Avatar asked May 05 '17 20:05

ilw


1 Answers

A system dialog is an activity. You can detect it by the top activity class name using ActivityManager.

final ActivityManager manager = (ActivityManager) context
    .getSystemService(Activity.ACTIVITY_SERVICE);

In devices with API Level less than 23 (M):

final List<ActivityManager.RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
final ComponentName componentName = runningTasks.get(0).topActivity;
final String className = componentName.getClassName();

if (className.equals("YOUR_EXPECTED_ACTIVITY_CLASS_NAME")) {
    // do something
}

In newer devices:

final List<ActivityManager.AppTask> appTasks = manager.getAppTasks();
final ComponentName componentName = appTasks.get(0).getTaskInfo().topActivity;
final String className = componentName.getClassName();

if (className.equals("YOUR_EXPECTED_ACTIVITY_CLASS_NAME")) {
    // do something
}

Or in this case, you can check if the device is in airplane mode before starting the activity:

private boolean isAirplaneModeOn(final Context context) {
    final int airplaneMode = Settings.System.getInt(
        context.getContentResolver(),
        Settings.System.AIRPLANE_MODE_ON,
        0
    );

    return airplaneMode != 0;
}

...

if (!isAirplaneModeOn(this)) {
    // do something
}
like image 135
Pedro Rodrigues Avatar answered Sep 19 '22 12:09

Pedro Rodrigues