Android实现屏幕保持常亮功能
作者:最萌小熊猫 时间:2023-08-09 13:20:15
本文实例为大家分享了Android实现屏幕保持常亮的具体代码,供大家参考,具体内容如下
一、需求背景
当我们在玩游戏或者看视频的时候不希望app黑屏,那么就需要在app使用的时候处于常量状态。
二、实现方案
1、使用PowerManager.WakeLock
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
if (powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLock");
wakeLock.acquire(); //开启屏幕常亮
//wakeLock.release(); //释放wakeLock,注意此方法可能出现异常.具体可看源码
}
2、MediaPlayer有自带的方法防止屏幕黑屏
mediaPlayer.setScreenOnWhilePlaying(true);
源码如下:
/**
* Control whether we should use the attached SurfaceHolder to keep the
* screen on while video playback is occurring. This is the preferred
* method over {@link #setWakeMode} where possible, since it doesn't
* require that the application have permission for low-level wake lock
* access.
*
* @param screenOn Supply true to keep the screen on, false to allow it
* to turn off.
*/
public void setScreenOnWhilePlaying(boolean screenOn) {
if (mScreenOnWhilePlaying != screenOn) {
if (screenOn && mSurfaceHolder == null) {
Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective without a SurfaceHolder");
}
mScreenOnWhilePlaying = screenOn;
updateSurfaceScreenOn();
}
}
......
private void updateSurfaceScreenOn() {
if (mSurfaceHolder != null) {
mSurfaceHolder.setKeepScreenOn(mScreenOnWhilePlaying && mStayAwake);
}
}
3、在view中设置
setKeepScreenOn(true/false),同样可以在xml中设置android:keepScreenOn="",只要View可见即生效。
4、通过添加flag的方法设置
/**
* 是否开启屏幕常亮
*/
private var enableKeepScreenOn = false
/**
* activity 生命周期 *
*/
private var mActivityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
//在activity创建的时候设置屏幕保持常亮
if (enableKeepScreenOn) {
activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
override fun onActivityStarted(activity: Activity) {
}
override fun onActivityResumed(activity: Activity) {
}
override fun onActivityPaused(activity: Activity) {
}
override fun onActivityStopped(activity: Activity) {
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
}
override fun onActivityDestroyed(activity: Activity) {
}
}
/**
* 设置保持屏幕常亮状态
*/
fun setKeepScreenOn(enable: Boolean) {
enableKeepScreenOn = enable
}
来源:https://blog.csdn.net/weixin_42600398/article/details/116402553
标签:Android,屏幕
0
投稿
猜你喜欢
java中Vector类的常用方法详解
2023-11-26 10:29:07
Java实现Android拼图游戏设计过程解析
2021-10-02 17:13:18
浅析Android加载字体包及封装的方法
2022-01-08 22:32:26
C# winform程序读取文本中的值实例讲解
2023-03-17 21:36:51
Java 数组交集的实现代码
2022-01-19 09:09:38
Java文件断点续传实现原理解析
2022-08-21 02:05:39
Java 8新的时间日期库的20个使用示例
2022-11-06 06:55:00
深入了解c#多线程编程
2022-08-14 15:13:58
SSH原理及两种登录方法图文详解
2023-11-14 11:10:53
C#中using语句的用法
2023-07-02 05:33:24
Java设计模式之抽象工厂模式
2022-05-30 12:45:16
Java中常见的查找算法与排序算法总结
2021-07-14 13:21:23
Spring中的注解@Autowired实现过程全解(@Autowired 背后的故事)
2023-01-01 06:02:53
Java并发编程之ReadWriteLock读写锁的操作方法
2023-12-07 20:08:37
C#画笔Pen绘制光滑模式曲线的方法
2021-11-26 05:13:33
Android实现拍照或者选取本地图片
2022-06-16 03:58:31
springboot @Valid注解对嵌套类型的校验功能
2023-07-01 13:02:18
spring aop execution表达式的用法
2023-08-22 05:39:24
一个简单的toolabar结合drawlayout使用方法
2023-05-12 15:01:16
JAVA设计模式之建造者模式原理与用法详解
2022-09-25 19:43:04