Turn off device screen (without locking it)? - android

I'm making an app that uses the Proximity Sensor while a phone call is in progress. My main issue is that the device doesn't turn the screen off when I hold it up against my face (while talking on the phone). If anyone has a solution as to how to fix this, then my problem will be solved.
Right now though, I'm trying to manually switch the screen off to imitate the normal behavior of the phone app. I looked through a lot of other posts about this but most of them seem to have instructions on how to keep the screen turned ON. My case is the exact opposite... how do I programmatically set the device to turn it's screen off? I don't want it to lock itself or sleep (since the phone call will still be in progress), just turn the screen completely off.
Thanks!

Related

How can I prevent Android Wear leaving ambient mode and going black after several minutes

I am trying to program a bedside clock like the Moto 360's showing the time during the night while charging.
Therefore I have set up an "Always-On-App" with alarm manager according to https://developer.android.com/training/wearables/apps/always-on.html#BackwardCompatibility which works basically as intended (Android 7.1.1 Wearable Activity on a "Diesel On Full Guard").
However after several minutes lying still on the desk, the watch is leaving ambient mode (my preferred mode during night time) and switching its screen off.
I've already tried using a wake lock according to Android Wear: measuring sensors and preventing ambient mode / sleep but with no success. The processor may still be working, but the screen goes black after a while (very bothersome to debug as you always have to wait and can't reproduce on the emulator, which never switches off).
So how can I prevent the watch from leaving ambient mode onto the direction "off", or at least detect that state and switch it back on?
Your watch probably has ro.ambient.plugged_timeout_min set to some positive value (see the reddit post here). It's meant for protecting the OLED screen from burn in. If you run adb shell dumpsys alarm you can see a wake alarm for com.google.android.wearable.ambient with the action com.google.android.wearable.action.STOP_AMBIENT_DREAM that will turn the screen off/doze device after the timeout mentioned above.
My best guess is that to prevent it from going to ambient you'd need to either
Stop it from going into ambient entirely, i.e holding a SCREEN_DIM_WAKELOCK or similar, and using an entirely black background.
Periodically wake the screen up so that the timeout resets. This is probably suboptimal if it's meant as a night clock though.

How to lock/unlock phone from Service in android?

I want to lock/unlock android using a service. I have checked other questions but none works. Some require to have my app in foreground and some I don't understand well enough. I want to show the lock screen if the device is password locked else home screen.
Just Turning screen on and off only will also work. Anyone know how to do it?
Edit 1:
I have tried things and managed to turn on(WakeLock) and off(DevicePolicyManager) screen. However, turning off screen does not lock phone only turns off the display. I use locknow(). This is acceptable but if possible can anyone tell how to lock phone?
Edit 2:
I can not lock my phone even by using physical lock button. It just turns off the screen.

How to turn off screen with smart lock Lollipop Pogramatically

Is there any way to lock the phone or turn the screen off and make it work with smart lock the next time I turn it on?
If I turn it off programatically the next time that I turn on the screen it dosen't start any smart lock process and looks like I didn't have the smart lock on.
Thanks
You can't do that currently. Using DevicePolicyManager.lockNow() to turn off the screen and lock the device crashes the GoogleTrustAgent. There is an open issue for this bug at the Android Open Source Project see Issue 79735. Hope it will be fixed soon.

Switch screen on by application (ANDROID, debug, all rights possible)

I read all there is about wake locks and so on. But I want something different. I have three devices to test, and mainly one device sucks really hard. Its the Asus transformer. The main problem, it can't be charged over USB. This means, if I tell it to "stay enabled during debug" the battery is dead after a while.
Also I want to write a small script deploying my app to all three devices once I build (no problem), starting them (no problem), and then ENABLE THE SCREEN (no way till now).
If found this intent, was happy for 23.5 seconds, and then read this:
http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON
Is there any way for me to enable the screen. The device is black, the screen is locked, my app starts (this works, seeing debug messages and hear startup sound), but i would have to manually unlock the screen.
I know, its not THAT bad, but when developing UIs I would have to do this like 100 times a day, and this is annyoing. The two little ones could stay on, but the big one (Transformer) would be empty after a while. I just had a thought, maybe I can tell the Transformer only to dim after a while, and to "undim" on startup. Would increase lifetime tenfold.
But still, if anyone knows a way to turn the screen on, that would be fantastic!
Chris
[UPDATE]
Ok, what you can do is reduce the screen brightness after some time, by using this:
WindowManager.LayoutParams WMLP = getWindow().getAttributes();
WMLP.screenBrightness = 0.01F;
getWindow().setAttributes(WMLP);
Dont use 0 as a value (at least not until this question is answered), because the screen will switch off, and you will never get in on again. At least without using your thumb and pressing a button, sooo oldschool...

Activate Android with touch instead of power button

We've got a tablet application for which we want to screen to timeout after a certain period, but would like the user to re-enable it by touching the screen instead of the 'power'-button on the side of the device.
What's the best approach (battery performance) to this?
Using a Partial Wake Lock (will the screen react?)
Disable the screen in some way, but keep everything (including touch response) working?
Other?
Any suggestions on this?
Gabriel,
Since this isn't possible by completely turning the screen off, you may want to just want to use a wake lock and dim the screen to its lowest setting as your 'timeout'. This way you can still accept screen touch events and 'wake' it from this state and bring it back to its previous brightness.
We've got a tablet application for which we want to screen to timeout after a certain period, but would like the user to re-enable it by touching the screen instead of the 'power'-button on the side of the device.
This is impossible except perhaps via custom firmware. If the screen is off, it will not respond to touch events.
Another thing you could do is use an app to turn screen off like ScreenStandby since it works in the background and can start on boot as well.
Out of curiosity, how can we go about contacting someone to help in creating a custom firmware like you did Gabriel?

Categories

Resources