4

I have an application that opens an Activity on a certain event.

Just like alarm application. I use the following flags:

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

The application creates a wake lock for that purpose.

On most devices it works fine, but on Xiaomi Redme 2(Android 6) it does not.

When the application launches the Activity, the screen is turned on and the lock screen displayed. My Activity is NOT displayed.

After I enter password I see my Activity.

Then I have changed the package name, and Activity displayed successfully without a lock screen.

It looks as though Xiaomi has black listed our original app for some reason.

I wonder if anyone has encountered this behavior and has a solution?

CodeBoyCode
  • 2,227
  • 12
  • 29

2 Answers2

5

This is a Xiaomi/MIUI specific issue. You need to grant special permission to an app to unlock the screen during alarm.

Go to System Settings > Permissions > Advanced Permissions > select the app and give it permission to access Lockscreen.

Source https://sleep.urbandroid.org/faqs/?Display_FAQ=22281

Pavel
  • 2,610
  • 3
  • 31
  • 50
  • Thanks, your answer is pretty helpful. But I have seen some other apps downloaded from Google Play Store have these special permissions already turned on by default in Xiaomi/MiUi devices. Can you help me in this case? – Osama Saeed Dec 07 '22 at 08:08
0

FLAG_SHOW_WHEN_LOCKED is deprecated. You can also use this in old api,

 final Window win= getWindow();

 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

And new usage, try this

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

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