Im trying to create a program to keep the keyboard backlight on if the screen is on. Im very new to android but i have been programming java for 6months. Im not sure how to use the constant Full_Wake_Lock to keep the kb lgiht on.
How do i use the constant Full_Wake_Lock in android4.0?
You would need to start a Service
.
Then you would have to acquire the wake lock within the onCreate, then in the onDestroy you would release the WakeLock. That is if you are trying to hold the wake lock from the background.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
that is to gain it, and then to release it:
wl.release();
And of course, you would want to declare wl
within the class body outside of any methods.
PowerManager.FULL_WAKE_LOCK is deprecated now, what should I use instead? –
Heterosexuality
See developer.android.com/reference/android/os/… –
Cetane
Thanks for answer. I've seen this link before, as you see it says that WakeLock was deprecated in API 17 and we should use FLAG_KEEP_SCREEN_ON, but it doesn't work correctly. When I use that flag, it doesn't turn the device on and the app can't start. Is that a big problem if we use a deprecated method or property in our app? –
Heterosexuality
I'd say you should be okay to use
FULL_WAKE_LOCK
at least for the next few updates of Android. API 17 is 4.2 if I'm not mistaken, which is on an extremely slim number of devices. Additionally, deprecated methods usually still function in updated version of Android (in my experience, anyway). –
Cetane Also, if you want to avoid using a deprecated method, you could acquire a partial wake lock along with using ACQUIRE_CAUSES_WAKEUP to wake up the device and turn the screen on. Not sure how efficient this would be compared to using the deprecated FULL_WAKE_LOCK –
Cetane
Thanks again. As the document's said, ACQUIRE_CAUSES_WAKEUP cannot be used with PARTIAL_WAKE_LOCK, but thank you very much for sharing your experience about deprecated methods. I've tested some ways, but if you need to wake the device up and bright the screen then showing an activity, the FULL_WAKE_LOCK and two other parameters is the only way works that I know! –
Heterosexuality
BEFORE: wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
AFTER: wakeLock = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");
© 2022 - 2024 — McMap. All rights reserved.