Is there any way to disable sleep of android mobile phone when user is in certain activity.
I want to disable screen time out when user is in main activity.
Is there any way to do it? Don't know if it is possible or not?
Is there any way to disable sleep of android mobile phone when user is in certain activity.
I want to disable screen time out when user is in main activity.
Is there any way to do it? Don't know if it is possible or not?
Try this.
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
I am using this:
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Don't forget to place the permission in the manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
Best way just add
android:keepScreenOn="true"
to the parent layout of that activity.
If you just want to prevent the sleep mode on a specific View, just call setKeepScreenOn(true) on that View or set the keepScreenOn property to true. This will prevent the screen from going off while the View is on the screen. No special permission required for this.
You can use the following code to keep screen on in a certain function as :
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
//..screen will stay on during this section..
wl.release();
This will get the power service and acquire its attention then release it after your code is done