博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
More about keeping Android’s screen awake
阅读量:6281 次
发布时间:2019-06-22

本文共 2092 字,大约阅读时间需要 6 分钟。

  hot3.png

In , I discussed the pros and cons of the various means of keeping the screen alive in Android, focusing mainly on the need for sandbox permissions in your application’sAndroidManifest.xmlfile.

In this post, I’d like to further elaborate on the “shortcut” techniques to allow you to keep the screen alive, without explicitly managing a Wake Lock in your application.

FLAG_KEEP_SCREEN_ON

I’ve already covered the technique of setting a flag on your Activity’s main window to keep the screen active:

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

There are two things to keep in mind with this approach:

  1. FLAG_KEEP_SCREEN_ONonly works when the app window is onscreen.  If the user switches apps, Android automatically releases the wake lock.  That might be fine for your use case, but if you have a long-running background task that really needs to keep the device running until finished, this isn’t an airtight approach.
  2. FLAG_KEEP_SCREEN_ONdoes just that; it keeps the screen active along with the rest of the device.  If you directly create and manage a raw wake lock in your own code, you have more granularity, for example allowing the screen to go dim or go off completely whilst still allowing the CPU to do its thing.

I won’t discuss how to create and manage a Wake Lock directly; instead, I’ll refer you to the and to a short (I happen to like the site for its nice, short and useful code snippets).

android:keepScreenOn

Although I haven’t tried this in my own dev work, you can also enforce “screen always on” behavior on a per-view basis.

Every view has an that you can use to keep the screen active whenever the view is visible.  This should be as easy as simply setting the attribute in the XML layout you use to define your views, plus it needs no Java code at all.

I suspectandroid:keepScreenOnworks by setting theFLAG_KEEP_SCREEN_ONon the window for you whenever the view is visible to the user. Anyway, this is handy to use if your need to keep the screen active is associated with a particular view in your app.

转载于:https://my.oschina.net/jerikc/blog/138804

你可能感兴趣的文章
TCP的三次握手四次挥手
查看>>
关于redis的几件小事(六)redis的持久化
查看>>
package.json
查看>>
webpack4+babel7+eslint+editorconfig+react-hot-loader 搭建react开发环境
查看>>
Maven 插件
查看>>
初探Angular6.x---进入用户编辑模块
查看>>
计算机基础知识复习
查看>>
【前端词典】实现 Canvas 下雪背景引发的性能思考
查看>>
大佬是怎么思考设计MySQL优化方案的?
查看>>
<三体> 给岁月以文明, 给时光以生命
查看>>
Android开发 - 掌握ConstraintLayout(九)分组(Group)
查看>>
springboot+logback日志异步数据库
查看>>
Typescript教程之函数
查看>>
Android 高效安全加载图片
查看>>
vue中数组变动不被监测问题
查看>>
3.31
查看>>
类对象定义 二
查看>>
收费视频网站Netflix:用户到底想要“点”什么?
查看>>
MacOS High Sierra 12 13系统转dmg格式
查看>>
关于再次查看已做的多选题状态逻辑问题
查看>>