This question already has an answer here:
- Force Screen On 3 answers
For my Android app I never want the phone to lock or the back light to turn off
This question already has an answer here:
For my Android app I never want the phone to lock or the back light to turn off
You need to use Power Manager to acquire a wake lock in your application.
Most probably you are interested in a FULL_WAKE_LOCK:
Add one line of code after setContentView() in onCreate()
Adding
android:keepScreenOn="true"
in the XML of the activity(s) you want to keep the screen on is the best option. Add that line to the main layout of the activity(s).Something like this
Have a look at this discussion Force Screen On
hackbod has a great answer.
Lots of answers already exist here! I am answering this question with additional and reliable solutions:
Using
PowerManager.WakeLock
is not so reliable a solution, as the app requires additional permissions.Also, if it accidentally remains holding the wake lock, it can leave the screen on.
So, I recommend not using the
PowerManager.WakeLock
solution. Instead of this, use any of the following solutions:First:
We can use
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
inonCreate()
Second:
we can use
keepScreenOn
1. implementation using
setKeepScreenOn()
in java code:Docs http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2. Adding
keepScreenOn
to xml layoutDocs http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
Notes (some useful points):
keepScreenOn
should be used on a Main/Root/Parent View. It can be used with any child view and will work the same way it works in a parent view.There are multiple ways you can do it:
Solution 1:
Solution 2:
In activity_main.xml file, simply add:
My advice: please don't use WakeLock. If you use it, you have to define extra permission, and mostly this thing is useful in CPU's development environment.
Also, make sure to turn off the screen while closing the activity. You can do it in this way: