Simulate an activity to postpone screen turn off - android

Each time an user touches screen, a timeout counter for turning screen off is reset.
How can I simulate an activity to reset such timeout counter programmatically, to postpone screen turning off?
I am not looking for keeping screen on permanently.
I am experiencing screen off issue when user is requested to use a fingerprint scanner and I want to reset such timeout counter each time when user's fingerprint is not recognized or any fingerprint sensor activity is recorded.

The solution you are looking for is already there in the Android documentation.
It is called Wake Lock. Keep the screen on programmatically using:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off. If you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags() like so:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Related

Android ACTION_SCREEN_OFF intent meaning

In Google documentation about ACTION_SCREEN_OFF they write:
This broadcast is sent when the device becomes non-interactive which may have nothing to do with the screen turning off. To determine the actual state of the screen, use Display.getState().
So I asking when the screen becomes non-interactive and it still turned on.
When you press the power button to lock the screen (or even if the device's screen lights get off, due to inactivity), at that time it goes to a non-interactive state.
And yes, the device will be turned on.

How to trigger an automatic screen lock process on my android phone after a specified process running specified time?

I want to control my android smartphone that after my son playing games on my phone after a specified time such as half an hour, the phone will be locked unless you submit the right password.
I don't know how to trigger the lock screen process.
How can I implement interrupting the games processes and lock the cellphone?
How unlock and lock screen programatically in android Has some excellent information on immediately calling the lock screen. You could implement a system using an alarm manager to trigger this event.
Here is a good example,
first thing you need to do this, U need to know when the screen gets unlock. So here you go.....
android-detect-phone-unlock-event-screen-on
After that you set a time for 30/40/50 minutes (as you wish).
Once the timer fires, use below link to lock your screen.
lock-phone-screen-programmtically
And your work is done.

Android: Workaround for light sensor firing every time the Power button is pressed?

I'm working on an Android app that turns a device's screen off when the light sensor value is below a threshold value and turns the screen on again when the light sensor value rises above the threshold value. This threshold value is decided by the user through a one-time calibration. Everything works great, the screen turns on and off when it's supposed to and I've dealt with the constant fluctuations in the light sensor's values so that the screen doesn't keep turning on and off randomly.
The issue I'm facing is that when the power button is pressed, the light sensor's "onSensorChanged" event is triggered for some reason. This means that if the user presses the power button to turn the screen off, it's automatically turn back on if it's in the right lighting.
The solution I thought of was to keep a flag variable that won't allow the code in my service to execute if the power button was pressed but the issue there is that Android does not allow me to detect a power button key press from a service.
Is there another possible (perhaps a more logical) workaround to this issue?
I don't think it would be helpful to post the code but let me know if you need me to post it.
Thanks.
Instead of tracking a flag for the power button press, why not keep a flag for when you turn it off? That way if it was turned off for any other reason, it won't do anything.
Edit: Another idea...
What you could do is track the last time the ACTION_SCREEN_OFF intent was fired. If the time is less than a certain threshold(0.05s?) from when your onSensorChanged is called, you can just disregard(or at least not turn it back on).

Can you programmatically check to determine if your android device is in deep sleep mode?

Can you programmatically check to determine if your android device is in deep sleep mode?
Reason being, if it is, you may obtain a wake lock or a wifi lock... however, is it is not, you wouldn't want to obtain a wifi lock if it is not necessary. Since, I also have a delay created when a wifi lock is obtained in order to allow the device time to connect to the network. If it is not needed, I do not want to get the wifi lock and add a delay.
I got a suggestion for you,
Register to screen off and on, when screen off take a one minute timer, if screen on cancel it, when you got to end of timer do your locks, till screen on again.

Is it possible to get a device to wake up from sleep (screen dark) by detecting a touch to the screen?

I want to get an android device to wake up from sleep or however the state in which the phone gets after a certain amount of inactivity when the screen goes dark, by detecting a touch to the screen instead of clicking on any button.
In the documentation the only thing I have found is the FLAG_TOUCHABLE_WHEN_WAKING flag in WindowManager.LayoutParams and it says:
Window flag: When set, if the device is asleep when the touch screen
is pressed, you will receive this
first touch event. Usually the first
touch event is consumed by the system
since the user can not see what they
are pressing on.
I thought that meant that if the device's screen is turned off and that flag is set for an Activity then it will wake up to the touch (which is what I want it to do). Am I misunderstanding the purpose of this flag? Are there additional implementation details I'm ignoring? Is there some other way?
Am I misunderstanding the purpose of this flag?
AFAIK, yes. There is a slice of time between when the screen turns off and when the device falls asleep. During this time, if the user touches the screen someplace where the window has this flag, the screen turns on again and the inactivity timer is reset.
I can find no other use of this flag in the Android source code.
Is there some other way?
No. If the device is asleep, touch screen events are not registered.

Categories

Resources