How to lock/unlock phone programmatically : Android
Asked Answered
L

3

10

In my application, I need to lock and unlock phone. I searched for it, I found many answers, I tried them but no one is working.

I need to unlock phone on onCreate() and lock phone again while I'm finishing my activity.

Do you guys know any method to do the same?

Thanks friends.

EDIT:

Some links, that I have tried are:

How to display Activity when the screen is locked?

Android screen lock/ unlock programmatically

https://groups.google.com/forum/#!topic/android-developers/BOLjJTMO4zE

In my application I'm scheduling a task using AlarmManager and then enabling the phone lock. My activity is getting started on scheduled time but it not unlocking my phone. when I'm unlocking my phone manually running activity is getting appeared

Limbic answered 29/7, 2013 at 12:25 Comment(4)
Which answers did you try so we know not to suggest them?Medusa
"no one is working" is not a useful description of your symptoms. Please explain completely and precisely what you have tried and what problems you encountered.Nematode
@BladeOrz: I have added some of visited pages linksLimbic
@CommonWare: I have added a little description about my problem. Thank youLimbic
W
16

on BroadcastReceiver set up the wakelock and in the activity

Do This:

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

import following

import android.view.Window;
import android.view.WindowManager.LayoutParams;
Whorton answered 8/9, 2014 at 13:43 Comment(2)
This somehow seems to work, and my application is brought to the front.... event though no sim card pin has been entered. I just somehow think that you will not be able to access any phone or data capabilities. I will let you know if this is the case...Gradygrae
Jip, confirmed. So you will not have access to the sim card functionality. So if you want to bypass entering the pin and fire up your app, this works great ... as long as the user doesn't minimize or close the app, cause then you're back at the login screen.Gradygrae
Z
7

As of API 27, FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON were deprecated. Instead use this in the Manifest

<activity
    android:name=".MyActivity"
    android:turnScreenOn="true"
    android:showWhenLocked="true"/>

Or, for general usage, in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setTurnScreenOn(true);
        setShowWhenLocked(true);
    } else {
        Window window = getWindow();
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    }
}
Zenaidazenana answered 9/2, 2018 at 19:37 Comment(0)
F
1

Try this...

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                    setContentView(R.layout.activity_main);
    }

    @Override
        protected void onPause() {
            super.onPause();
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);


        }
Fogarty answered 20/12, 2013 at 6:48 Comment(1)
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD was deprecated in API 26 or 27.Wishywashy

© 2022 - 2024 — McMap. All rights reserved.