XIaomi device does not dismiss lockscreen when opening Activity from background (alarm event)
Asked Answered
R

2

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?

Risner answered 26/9, 2018 at 13:26 Comment(0)
B
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

Bathy answered 1/10, 2018 at 11:40 Comment(1)
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?Palaeogene
H
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);
        }
Hammett answered 21/9, 2022 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.