android 休眠参数,Android屏幕亮度、待机时间总结

程序员成长笔记

屏幕亮度部分

//设置屏幕亮度

private fun changeAppBrightness(context: Activity) {

val window = context.window

val lp = window.attributes

if (isAutomatic) {//是否跟随系统

lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE

} else {

lp.screenBrightness = (if (screenLightValue <= 0) 1 else screenLightValue) / 255f

}

window.attributes = lp

}

val screenLightValue: Int//获取屏幕亮度,例子中使用srp保存亮度值

get() = SRPreferences.instance.getInt(SRPreferences.READ_LIGHT_VOLUE, 122)

fun setScreenLightValue(activity: Activity, value: Int) {//设置亮度值,并改变屏幕亮度

SRPreferences.instance.setInt(SRPreferences.READ_LIGHT_VOLUE, value)

changeAppBrightness(activity)

}

private val screenLightMode: Int//获取屏幕亮度模式,跟随系统or手动控制

get() = SRPreferences.instance.getInt(SRPreferences.READ_LIGHT_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)

fun setScreenLightMode(activity: Activity, screenLightMode: Int) {//设置亮度模式,并改变屏幕亮度

SRPreferences.instance.setInt(SRPreferences.READ_LIGHT_MODE, screenLightMode)

changeAppBrightness(activity)

}

val isAutomatic: Boolean//是否跟随系统

get() = screenLightMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC

以上代码简单封装常用的屏幕亮度控制,提供简单的页面亮度模式切换和页面亮度改变。使用srp做部分的数据持久化,以便做为软件的持久设置,如不需要,可更改为静态变量。

屏幕待机时间部分

fun syster(reset: Boolean) {//记录系统待机时间或还原

try {

if (reset) {

Settings.System.putInt(App.app!!.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT,

SRPreferences.instance.getInt(SRPreferences.READ_LIGHT_TIME_SYSTER, 10*60*1000))

} else {

SRPreferences.instance.setInt(SRPreferences.READ_LIGHT_TIME_SYSTER,

Settings.System.getInt(App.app!!.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT))

}

} catch (e: Exception) {

e.printStackTrace()

}

}

fun setWindowLightTime(activity: Activity, value: Int) {//设置屏幕待机时间

SRPreferences.instance.setInt(SRPreferences.READ_LIGHT_TIME, value)

activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

when (value) {

0 -> syster(true)

5 -> Settings.System.putInt(App.app!!.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, 15 * 1000)

10 -> Settings.System.putInt(App.app!!.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, 30 * 1000)

-1 -> activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

}

}

这部分主要有两个方法:

第一个方法主要是记录系统待机时间,以及还原系统待机时间。当参数为true时,是还原系统时间,前提是已经记录过系统时间,否则会设置为默认时间10分钟。

第二个方法主要是设置待机时间,可选项以及时间可以自己定制,现在的方法中主要有4个case,当值为0的时候即设置为系统时间,值为-1的时候会设置为常亮,值为5和10的时候回分别设置为不同的待机时间。

同样的使用srp做一些数据的持久化处理。

使用的时候一般会在进入程序时记录一下系统时间值,然后读取上次记录的用户选择值,并做设置;在退出程序的时候还原为系统时间值。

最后记录一个自己用的倒计时的工具类:

abstract class Timer {

private var mMillisInFuture: Long = 0

private val mCountdownInterval: Long = 1000

private var mStopTimeInFuture: Long = 0

private val mPauseTimeInFuture: Long = 0

private var isStop = false

private var isPause = false

fun setMillisInFuture(millisInFuture: Long): TTSTimer {

stop()

mMillisInFuture = millisInFuture

return this

}

@Synchronized private fun start(millisInFuture: Long): TTSTimer {

isStop = false

if (millisInFuture <= 0) {

onFinish()

return this

}

mStopTimeInFuture = SystemClock.elapsedRealtime() + millisInFuture

mHandler.sendMessage(mHandler.obtainMessage(MSG))

return this

}

/**

* 开始倒计时

*/

@Synchronized fun start() {

start(mMillisInFuture)

}

/**

* 停止倒计时

*/

@Synchronized fun stop() {

isStop = true

mHandler.removeMessages(MSG)

}

/**

* 重新开始

*/

@Synchronized fun restart() {

if (isStop || !isPause)

return

isPause = false

start(mPauseTimeInFuture)

}

/**

* 倒计时间隔回调

* @param millisUntilFinished 剩余毫秒数

*/

abstract fun onTick(millisUntilFinished: Long)

/**

* 倒计时结束回调

*/

abstract fun onFinish()

private val mHandler = object : Handler() {

override fun handleMessage(msg: Message) {

synchronized(this@Timer) {

if (isStop || isPause) {

return

}

val millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime()

if (millisLeft <= 0) {

onFinish()

} else {

val lastTickStart = SystemClock.elapsedRealtime()

onTick(millisLeft)

var delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime()

while (delay < 0)

delay += mCountdownInterval

sendMessageDelayed(obtainMessage(MSG), delay)

}

}

}

}

companion object {

private val MSG = 1

}

}

只是一个抽象类,具体使用:

var mTimer: Timer = object : Timer() {

override fun onTick(millisUntilFinished: Long) {

//倒计时回调

}

override fun onFinish() {

//结束回调

}

}

mTimer.setMillisInFuture((30 * 60 * 1000).toLong()).start()

更新:修改屏幕休眠时间的方法有bug,在高版本手机获取修改系统设置的权限并不太容易,所以在另一篇文章中介绍了使用powermanager修改屏幕休眠时间的方式,可以参考一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值