Keeping screen on, which way?

こ雲淡風輕ζ 提交于 2019-12-19 05:44:06

问题


I have found two ways on keeping the screen on:

First one is simpler:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Second one is using a wakelock and requiring an extra permission:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");

Is there any real difference between this two methods apart from the second one being more complicated to implement and requiring an extra permission? Will the end result be always the same?


回答1:


You should see Coding for (Battery) Life Google IO presentation, slide 16

If you don't want to, then: You could do the first one in the XML for any layout element and it is the suggested one to use (don't know about applying it to the window though, might be as bad as the wakelock, dunno).

XML:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true">

Window Flag:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


来源:https://stackoverflow.com/questions/2771811/keeping-screen-on-which-way

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