androidwindowfloatingandroid-12

How to show android Floating Window even on the Android 12's System Setting UI?


After upgrade to target SDK 30, my AccessibilityService's Floating Window can't show on Android 12's System Setting UI. When enter the System Setting UI, Floating Window disappears. After quit the System Setting UI, Floating Window appears again.

I think it's a new limitation on android 12, before it's ok. Anyone have a workaround? as I really need to show some tips while doing in AccessibilityService. thank you.

addView:

    if (mWindowManager != null) {
        mWindowManager.addView(buildAnimWindow(), buildLayoutParams());
    }

buildAnimWindow:

public AnimWindow buildAnimWindow() {
    AnimWindow res;

    LayoutInflater inflater = LayoutInflater.from(mContext);
    res = (AnimWindow) inflater.inflate(R.layout.anim_window, null);

buildLayoutParams:

public WindowManager.LayoutParams buildLayoutParams() {
    WindowManager.LayoutParams res;

    res = new WindowManager.LayoutParams();
    if (Build.VERSION.SDK_INT >= 29) {
        res.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
    } else if (Build.VERSION.SDK_INT >= 26) {
        res.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        res.type = WindowManager.LayoutParams.TYPE_PHONE;
    }

Solution

  • ForegroundService did the trick, tested ok on android 12:

    MyForegroundService:

    public class MyForegroundService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
            startForegroundFullOs(NOTIFY_ID, buildNotify());
    
            init();
        }
    
        private Context getContext() {
            return MyForegroundService.this;
        }
    
        private void init() {
            Logger.d(TAG, "BPFS::init");
            mContext = getContext();
    
            if (Build.VERSION.SDK_INT >= 29) {
                if (MyService.getInstance() != null) {
                    mWindowContext = MyAccessibilityService.getInstance();
                } else {
                    mWindowContext = getContext();
                }
                mWindowManager = (WindowManager) mWindowContext.getSystemService(Context.WINDOW_SERVICE);
            } else {
                mWindowContext = getContext();
                mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            }
        }
    
        @SuppressLint("InflateParams")
        public MyAnimWindow buildMyAnimWindow() {
            MyAnimWindow res;
    
            LayoutInflater inflater = LayoutInflater.from(mWindowContext);
            res = (MyAnimWindow) inflater.inflate(R.layout.anim_window, null);
            Logger.d(TAG, "BPFS::buildMyAnimWindow, res = " + res);
    
            return res;
        }
    
        public WindowManager.LayoutParams buildLayoutParams() {
            WindowManager.LayoutParams res;
    
            res = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= 29) {
                res.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
            } else if (Build.VERSION.SDK_INT >= 26) {
                res.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                res.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            res.format = -3;
            res.width = -1;
            res.height = -1;
    
            res.flags = 0;
            res.flags |= 262144;
            res.flags |= 1024;
            res.flags |= 256;
            res.flags |= 8;
            res.flags |= Integer.MIN_VALUE;
    
            res.gravity = 51;
            res.x = 0;
            res.y = 0;
    
            return res;
        }
    
        
        public void showMyAnimWindow() {
            try {
                if (mWindowManager != null) {
                    mWindowManager.addView(buildMyAnimWindow(), buildLayoutParams());
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    
    }
    

    MyAccessibilityService:

    public class MyAccessibilityService extends AccessibilityService {
    
        private static MyAccessibilityService sInstance;
        public static MyAccessibilityService getInstance() {
            return sInstance;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            sInstance = MyAccessibilityService.this;
        }