Android Keep Screen On In App

余生长醉 提交于 2019-11-29 15:39:49

Try this:

mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,         
                            "");    
mWakeLock.acquire();

You might use flag FLAG_KEEP_SCREEN_ON

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and when you don't want to keep on

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

This way is much better, bcz of using less permissions, services and ofc battery:

public void setWakelock(Activity Target, boolean State)
{
    if (State) Target.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    else Target.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    return;
}

EDITED

Follow the pattern Mark Murphy provides with the WakefulIntentService. and you can find the explanation in his book very good one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!