Native.JS监听手机锁屏,解锁,开屏

var receiver;
mui.plusReady(function() {

main = plus.android.runtimeMainActivity(); //获取activity
// 广播接收
var ALARM_RECEIVER = "alarm_receiver";
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
var Intent = plus.android.importClass('android.content.Intent');
console.log(intent.getAction());
//action = intent.getAction();
if(intent.getAction() == Intent.ACTION_SCREEN_ON) {
console.log('开屏')

} else if(intent.getAction() == Intent.ACTION_SCREEN_OFF) {
console.log('锁屏')

} else if(intent.getAction() == Intent.ACTION_USER_PRESENT) {
console.log('解锁')
\n

}
result.textContent += '\nAction :' + intent.getAction();
main.unregisterReceiver(receiver);
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
main.registerReceiver(receiver, filter); //注册监听

})

代码已测试过了ctrl+c直接使用


2 分享 关注
keoy DCloud_heavensoft
小生复小生

小生复小生

result.textContent += '\nAction :' + intent.getAction();这句话没用吧,但是我这边有这句话报错:result is not defined。 三个广播都能收到。但是注释掉这句话不报错了,但是只能监听到锁屏广播,另外两个就没反应了。
0 赞 4 天前
拜仁已伤

拜仁已伤 回复 wangsongbai1231@163.com

uniapp在App.vue里面有onLaunch onShow onHide监听方法
0 赞 2018-12-03 16:44
wangsongbai1231@163.com

wangsongbai1231@163.com

uniapp里没法用吧?
0 赞 2018-11-30 20:23
1679726649@qq.com

1679726649@qq.com

好像有bug
0 赞 2018-11-30 20:21
1679726649@qq.com

1679726649@qq.com

好像有bug
0 赞 2018-11-30 20:21
keoy

keoy

好棒
0 赞 2018-11-08 16:22

要回复文章请先登录注册