Settings.canDrawOverlays() always returns false on Android O

·2 min read·paskov

I was updating an app for one of my clients and testing its behavior on Android O (API 26). The app needs android.permission.SYSTEM_ALERT_WINDOW to draw over other apps.

On Android 6 (API 23) and later, apps targeting API 23 or higher must request this special permission through system settings.

First, call Settings.canDrawOverlays() to check whether permission has already been granted. If it returns false, open the overlay permission settings so the user can choose whether to allow the app to draw over other apps.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    // On API 23 and later, ask the user to grant overlay permission if needed.
    if (!Settings.canDrawOverlays(this))
    {
        Intent intent = new Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName()));

        startActivityForResult(intent, REQUEST_PERMISSION_SYSTEM_OVERLAY_RESULT);
    }
}

On Android O (API 26), I encountered a bug that caused Settings.canDrawOverlays() to keep returning false, even after the user had granted overlay permission. The workaround below is a little awkward, but it avoids making the user restart the app after granting permission.

The code below first checks Settings.canDrawOverlays(). If it returns true, the app continues its normal flow. If it returns false on Android O (API 26), the code calls our canDrawOverlays() helper as a fallback.

The helper tries to add an invisible overlay window and then remove it. If both operations succeed, it returns true. If the window manager is unavailable or an exception occurs, it returns false.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;

    if (requestCode == REQUEST_PERMISSION_SYSTEM_OVERLAY_RESULT)
    {
        if (Settings.canDrawOverlays(this))
        {
            m_permissionSystemOverlayWindowGranted = true;

            if (m_permissionReadPhoneStateGranted && m_permissionProcessOutgoingCallsGranted)
            {
                startService(new Intent(this, EstatePlusService.class));
                m_layoutNoPermissions.setVisibility(View.INVISIBLE);
                m_progressBar.setVisibility(View.INVISIBLE);
                m_layoutLogin.setVisibility(View.VISIBLE);
            }
        }
        else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
        {
            // Work around the Android O bug that can report false after permission is granted.
            if (canDrawOverlays(this))
            {
                m_permissionSystemOverlayWindowGranted = true;

                if (m_permissionReadPhoneStateGranted && m_permissionProcessOutgoingCallsGranted)
                {
                    startService(new Intent(this, EstatePlusService.class));
                    m_layoutNoPermissions.setVisibility(View.INVISIBLE);
                    m_progressBar.setVisibility(View.INVISIBLE);
                    m_layoutLogin.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

/**
 * Workaround for Android O.
 */
public static boolean canDrawOverlays(Context context)
{
    try
    {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null)
        {
            return false;
        }

        final View viewToAdd = new View(context);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            0,
            0,
            android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O
                ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
        viewToAdd.setLayoutParams(params);
        windowManager.addView(viewToAdd, params);
        windowManager.removeView(viewToAdd);
        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return false;
}

Comments 1

  1. Neville

    It is not a bug. In Android O, It doesn’t allow “SYSTEM_ALERT_WINDOW”