vue app hide 后台运行代码
<script>
export default {
data() {
return {
g_wakelock : null
}
},
methods: {
wakeLock() {
var g_wakelock = this.g_wakelock;
var main = plus.android.runtimeMainActivity();
var Context = plus.android.importClass("android.content.Context");
var PowerManager = plus.android.importClass("android.os.PowerManager");
var pm = main.getSystemService(Context.POWER_SERVICE);
g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
g_wakelock.acquire();
console.log("=================");
} ,
releaseWakeLock () {
var g_wakelock = this.g_wakelock;
if(g_wakelock != null && g_wakelock.isHeld()) {
g_wakelock.release();
g_wakelock = null;
}
}
},
onLaunch: function() {
console.log('App Launch')
uni.getLocation({
success: function (res) {
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
}
})
},
onShow: function() {
console.log('App Show')
this.releaseWakeLock();
uni.getLocation({
success: function (res) {
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
}
})
},
onHide: function() {
console.log('App Hide')
this.wakeLock();
this.timer = setInterval(() => {
uni.getLocation({
success: function (res) {
console.log('当前位置的时间:' + new Date());
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
}
})
}, 1000);
}
}
</script>
<style>
/*每个页面公共css */
</style>