Question

I'm working on an app that requires the screen to be locked with a particular activity when under certain conditions. Specifically, I have a service that runs in the background checking for GPS speed, and if the speed becomes greater than a defined amount, I need to open an activity that cannot be exited until the speed falls below the threshold amount. I'm relatively new to android development, this is a project for a class, we haven't discussed wake locks or power managers yet but i'm happy to learn if that's what I need to do.

The code I have is pretty irrelevant for this question I think, since what I'm trying to do is separate from the code I already have but I can edit and supply it if need be. Thank you.

Was it helpful?

Solution

In short: no, it's not possible.

Longer answer -- it depends on what you mean by "lock the screen". When people hear that phrase, they typically think it means to lock the device such that the user cannot do anything else -- the security screen that prevents them from going far when they turn the device on (until they satisfy the screen with a pin, pattern, facial recognition, etc).

In your question you mention wake locks and the power manager -- which is not significant in the security lock screen; this is significant if what you really meant was "lock the screen on", or "don't allow the screen to auto-turn off" while this activity is active. If that's what your intention is, then yes, you can do that. From How to get an Android WakeLock to work?, you'll need a permission in your manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

and you'll need code resembling the following to enable your wake lock:

private PowerManager.WakeLock wl;

    protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");
    }//End of onCreate

            @Override
        protected void onPause() {
            super.onPause();
            wl.release();
        }//End of onPause

        @Override
        protected void onResume() {
            super.onResume();
            wl.acquire();
        }//End of onResume

while the following would disable it when you're ready:

w1.release()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top