Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use instead of deprecated FLAG_SHOW_WHEN_LOCKED flag to start activity when phone is locked in android

I want broadcast to start an activity even when the phone is locked. What I have found is that there are window flags FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON that do exactly what I need. The problem is that they are deprecated in the latest versions of android.

Also I found out that setShowWhenLocked(true) and setTurnScreenOn(true) methods can be used but they give me an error "No virtual method setShowWhenLocked(Z)V in class Someclass"

Could you suggest how I could achieve the needed outcome?

like image 731
Dave Avatar asked Jan 02 '23 07:01

Dave


1 Answers

Use activityObj.setShowWhenLocked (true) instead of FLAG_SHOW_WHEN_LOCKED

Use setTurnScreenOn() instead of FLAG_TURN_SCREEN_ON

Note: These methods are only available from API level 27. So you need to perform some checking prior to using it

if (Build.VERSION.SDK_INT >= 27) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
    } else {
        this.window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
    }
like image 117
Sagar Avatar answered Feb 27 '23 04:02

Sagar