0

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.

Need-4-Steve
  • 77
  • 1
  • 1
  • 6

1 Answers1

1

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()
Community
  • 1
  • 1
mah
  • 39,056
  • 9
  • 76
  • 93
  • thank you for your insightful answer. I did mean "lock it" so that they can't do anything else, so I guess that answers that question, but I would also like to activate a screen using this wake lock. My follow up question is, can I start this wake lock from the android being in standby, and by standby i mean when it is dimmed from inactivity. – Need-4-Steve Jan 17 '14 at 19:36
  • If this is possible, it's likely through setting up a broadcast receiver to receive an event that tells you the dimming is happening; your receiver would start the activity (which would probably use a wake lock to remain on). That said, I'm not aware of an appropriate event to receive. I know you can receive android.intent.action.SCREEN_OFF but that might be too late (especially if an immediate lock is enabled). http://stackoverflow.com/questions/4031740/where-is-the-all-android-broadcast-intent-list/15178266#15178266 has an incomplete list of receivable events. – mah Jan 18 '14 at 11:58