Context: I'm under Android 4.0.4.
The code I'm stating is inside an Activity.
The application is a Cordova application, I'm writing this code inside a plugin.
I think I read all the questions here in StackOverflow and the Android docs.
I want to acquire a wakelock, wake up the device, do some stuff and then, when the wakelock is released, i want the screen to turn off again and let the phone sleep.
I have tried several approaches:
I tried several examples found here to wake up the device.
The only way I managed to turn the device on programatically was aquiring a wakelock with the ACQUIRE_CAUSES_WAKEUP flag. This works fine. The problem comes when I release the wakelock. There's no way I can turn the screen off again. It just stays on forever. I tried with all the WAKELOCKS available and with all the FLAGS available.
Note:I'm not totally happy with this approach because as stated in the docs, the only wakelock that is not deprecated is the partial wakelock, which unfortunately can't be used with this flag.
As I couldn't find any way of letting the screen off using the wakelocks functionality I tried to turn it off programmatically.
I think I tried all the examples found here to turn the screen off, and the only one which worked was:
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
The problem is that using this method I can't turn it on again. When I acquire the wakelock again it just stays off.
Also, when I try to wake up the phone pressing the "home" button it doesnt wake up. It only responds after pressing 3 or 4 times the "power" button. Seems like this approach freezes the phone.
I tried to turn on the screen programmatically before acquiring (and before too) the wakelock, restoring the default brightness value using the same code as above, but It doesn't work.
It's almost a week that I'm struggling with this. Any piece of advice will be appreciated.
Related
I am using the following codes in my app to keep my device screen from timing out/shutting off. I have used right permissions in my Manifest.
Screen Keep On:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Screen Off:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Those codes work if the app is running. However, when I close the app the flags seem to clear automatically leaving my device to timeout as normal. I want to keep screen on even if the app is closed. I've tried looking into Wakeful Receiver service but I can't seem to get it to work right. Also, I noticed Wakeful Receiver was depreciated in API 26. Any ideas? Thanks in advance!
My android app is able to run on the bakground and I'd like it to do so wenever the user stops interacting with the phone.
I the app is open and the user does nothing with it for x time, I expected Android to lock the screen (which is what i want).
However, the app simply keep th screen on. Why?
I do not use android:keepScreenOn="true" anywhere on the app.
Are you acquiring a WakeLock ?
If yes you should use PARTIAL_WAKE_LOCK instead FULL_WAKE_LOCK or SCREEN_BRIGHT_WAKE_LOCK the first lets the screen go off, the others don't
Well, turns out it was my fault, not the apps.
I had enabled 'Keep screen awake while charging', and since I had the phone connected to the computer, the screen never locked...
I'll leave this here in case someone else is in the same situation.
I have a Service that keeps the display on at a dim level at certain times, and it uses a 'dim' WakeLock to accomplish this. It works well... except that the screen never locks. That is, while the dim WL is held, the lock screen never appears requiring the user to swipe and authenticate.
Note that I'm developing on a platform that may have vendor changes to the low-level Android Java framework code, so this might not be standard Android behavior. But also, I have access to the framework code and can change it, if necessary. I just can't figure out where this policy is enforced in the code.
When the device is on external power, we want to keep the screen contents visible - but we still want it to lock.
The only way I can think of to do that is for you to maintain your own timer for when to trigger the lock, then to use DevicePolicyManager and lockNow() to lock the device at that point. This requires extra permissions and extra setup work (enabling your app as a device manager).
I'm using the examples from this site: http://android.konreu.com/developer-how-to/vibration-examples-for-android-phone-development/
So, as you see, I'm using the Vibrator Service. The problem, which also is written on the site, is that the vibration don't work if the screen has timed out.
Anyone has any ideas to fix this? It surely should work as the vibration works on phone calls, alarm etc. even though the screen is black. But maybe I have to wake up the phone before the vibration is called?
See this page about acquiring a wake lock. Hopefully it will do what you want.
I'm just wondering if I do this correctly; I'm programming a notification app which can display a notification when the phone is sleeping
Disable keyguard lock
Aquire a wake lock
show notification
Set alarm for timeout and reenabling keyguard and release wakelock is the user dont touches the screen.
4.1 User touches the screen, and I disable the timer. Do nothing more. Done and done
4.2 User dont touch the screen, so reenable keyguard and release wakelock. Phone sleeps again
Basically I'm wondering about point 4.1 the most. cancel the pendingintent for the alarm, and do nothing more? or should the keyguard and wakelock that are set be dealt with in some way?
The trick to implement your own Keyguard-replacement appears to be the following:-
In the onCreate method, you don't disable the keyguard, but the user can interact with the screen at this point so you need to be careful about accidental touches.
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED |
LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_KEEP_SCREEN_ON);
mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
If the user performs some action to indicate they want to interact more fully, then we can disable the keyguard and move on.
mKeyguardLock.disableKeyguard();
mKeyguardManager.exitKeyguardSecurely(null);
If they don't, then since the keyguard isn't disabled you shouldn't need to do anything more, just finish your activity
Thats it, but I'm still testing it. So, I'm not 100% sure about it.
I know this question is old but the API clearly state that programatically trying to acquire a keyguard unlock is deprecated.
The correct strategy is, in the oncCreate method of your activity, to have:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Hope this helps others.
Locks are global and when one appplication acquires it it will be off until the lock is released.
You should always reenable locks. Otherwise system won't go sleep or lock from Home or any other application
EDIT: I'm not really shure how it works with keyguard :/