Different behaviour when app is screen pinned

123 Views Asked by At

My app never goes to sleep while power is connected and if power is removed then the tablet goes into sleep. After powercable is reinserted the app is getting launched automatically when power is connected (Battery is charging). To achive this I have implemented a BroadcastReceiver and within this Receiver I (re-) launch my app with:

if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
                // Start the MainActivity

                    Intent i = new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(i);
            }

In the MainActivity I use (in onCreate()):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
            setTurnScreenOn(true);
        }
        else{


        }

        if( something...) {
            final Window win = getWindow();
            win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Object o = this.getSystemService(Context.KEYGUARD_SERVICE);
            if (o != null) {
                KeyguardManager keyguardManager = (KeyguardManager) o;
                keyguardManager.requestDismissKeyguard(this, null);

            }
        }
        else {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }

This works fine as long as the app is not screen-pinned. If the app is screen-pinned then this works only once. I assume because the app gets launched from screen-pinned context ?? Any further plugging of the powercable launches the app but is getting overlayed by a battery charge % window which dissapears after ca. 5 seconds and then the tablet goes into sleep. Is there a difference in this context when the app is screen-pinned ?

0

There are 0 best solutions below