I am Building a android application in which i want screen(activity) not locked after some time means application screen always on.How can i do this in my application for all screen keep always on.means no screen ssaver no lock screen overcome in my application.
Asked
Active
Viewed 8,420 times
-2
-
1You can refer to this post, next time search. [Solution][1] [1]: http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app – noidraug Apr 29 '14 at 07:03
4 Answers
6
try below code:-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
or
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
for more info see below links:-
-
thankyou sir one more question in i want when phone start my application run with phone boot.doese not exit from my application while phone switch off – user3541223 Apr 29 '14 at 07:33
-
-
My requirement is when android phone is turned on run my application only on phone startup.and user never exit from my application until phone is turned off – user3541223 Apr 29 '14 at 07:44
2
simply add this line in onCreate
method
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
It will keeps screen in active state.

Shailendra Madda
- 20,649
- 15
- 100
- 138
0
Do something like below,
protected PowerManager.WakeLock wakelock;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstances) {
setContentView(R.layout.main);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.wakelock= pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.wakelock.acquire();
}
@Override
public void onDestroy() {
this.wakelock.release();
super.onDestroy();
}
don't forget to add the following permission in manifest file :
<uses-permission android:name="android.permission.WAKE_LOCK" />

Spring Breaker
- 8,233
- 3
- 36
- 60
0
These are the ways to keep the screen on: http://developer.android.com/training/scheduling/wakelock.html

Karakuri
- 38,365
- 12
- 84
- 104