微信小程序识别息屏
微信官方给的onHide()的执行条件是“小程序从前台进入后台时触发”,就是只要用户看不见这个页面,包括离开小程序,离开此页面,离开微信,熄灭屏幕等等都会被调用。
官方文档有一个获取屏幕亮度的函数
https://developers.weixin.qq.com/miniprogram/dev/api/device/screen/wx.getScreenBrightness.html
在onhide里判断屏幕的亮度,息屏的时候亮度会是0(注意:安卓暂不支持该种操作,因为安卓系统息屏的时候屏幕亮度并不会至零,还是息屏前的值)
// 当检测到系统为iOS时,开启离开提示功能
onHide: function() {
var that = this;
wx.getSystemInfo({
success: function(res) {
that.setData({
systemInfo: res,
})
if (res.platform == "ios") {
wx.getScreenBrightness({
success: function(res) {
console.log("当前屏幕亮度:" + res.value)
if (res.value !== 0) {
$wuxDialog().alert({
resetOnClose: true,
maskClosable: false,
closable: false,
title: '失败',
content: "因为您在任务期间退出了该页面,此次任务失败",
onConfirm(e) {
wx.switchTab({
url: "../today/today"
})
},
})
}
}
});
} else if (res.platform == "android") {
}
}
})
},
还没有评论,来说两句吧...