androidflagslockscreenkeyguard

Can't get my Activity in front of the lock screen


I want to put my activity in front of the lock screen. I added whatever I saw on the internet as a flag to print activity on the lock screen.

But on some devices I couldn't achieve this : Huawei, Xiaomi etc

public class MyActivity extends AppCompatActivity() {

private void adjustScreen() {
 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | 
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        
        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    setShowWhenLocked(true);
    setTurnScreenOn(true);
        
    (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE)
    .requestDismissKeyguard(this, null);
  }
}

Manifest:

 <activity
   android:name=".MyActivity"
   android:configChanges="locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|screenSize|fontScale"
   android:excludeFromRecents="true"
   android:exported="true"
   android:launchMode="singleTask"
   android:screenOrientation="portrait"
   android:showOnLockScreen="true"
   android:showWhenLocked="true"
   android:turnScreenOn="true"/>

Solution

  • It only worked with these lines:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);