我在试着写一个"feel the watch">tizen的替代方案(一个用于tizen可穿戴的web tizen应用程序)。
我想让手表振动:
一个- 当分钟为:15时振动 两个>当分钟为:30时振动
- 3 当分钟为:45时振动
我已经尝试了很多方法,如"alarm api"等等,但是worker方法可以正常工作。我使用tizen.power.request("CPU"," cpu_wake ");但是手表显示我的网页应用的电池使用量很高。
如果应用程序在后台没有打开的情况下振动会更好。
这是我的树:
│ .tproject
│ config.xml
│ icon.png
│ index.html
│ main.js
│ reicon.jpg
│ reicon_crop.jpg
│ VibrateWatch.wgt
│
├───.settings
│ .jsdtscope
│ org.eclipse.wst.css.core.prefs
│ org.eclipse.wst.jsdt.ui.superType.container
│ org.eclipse.wst.jsdt.ui.superType.name
│
├───.sign
│ .manifest.tmp
│ author-signature.xml
│ signature1.xml
│
├───css
│ style.css
│
└───js
main.js
worker.js
这是我的worker.js代码:
function timedCount() {
postMessage(0); //send data
setTimeout("timedCount()",1000); // set vibration interval (or use specific time)
}
timedCount();
这是我的main.js代码:
//786
var pause = 150;
var dot = 300;
var ldot = 900;
var vibratePattern;
var tout;
var lastmin = 61;
var currentTime, timeLeft;
function format2Digits(value){
if(value < 10)
return "0" + value;
return value;
}
function capp() {
var currApp = tizen.application.getCurrentApplication();
currApp.hide();
}
function multiVibration(list) {
tizen.application.launch("MWoMC5yuQf.VibrateWatch", onsuccess);
console.log(onsuccess);
function onsuccess() {
console.log("Application launched successfully");
}
/* Vibrate SOS */
setTimeout(function (){ navigator.vibrate(list);
}, 1300);
setTimeout("capp()",5000);
}
window.onload = function () {
var worker = new Worker("js/worker.js");
worker.onmessage = secondly;
};
function secondly(){
tizen.power.request("CPU","CPU_AWAKE");
var date = tizen.time.getCurrentDateTime();
var houres = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
console.log('Time:',houres,minutes,seconds)
currentTime = document.querySelector("#currentTime");
currentTime.innerHTML = format2Digits(houres) + ":" + format2Digits(minutes) + ":" + format2Digits(seconds);
if ( ((0 <= seconds) && (seconds <= 10)) && (lastmin !== minutes) ){
setTimeout(function (){
switch (minutes) {
case 15:
tout = 700
setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
vibratePattern = [dot];
multiVibration(vibratePattern);
break;
case 30:
tout = 700
setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
vibratePattern = [dot, pause, dot];
multiVibration(vibratePattern);
break;
case 45:
tout = 700
setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
vibratePattern = [dot, pause, dot, pause, dot, pause];
multiVibration(vibratePattern);
break;
}
}, 500);
lastmin = minutes;
console.log('LM',lastmin);
}
}
如果你有什么想法,请告诉我。谢谢:)有几个概念可能对你有用。
- 应用程序的后台支持-当设备需要振动时,您不需要每次都raise UI。你可以配置一个background-support和background-category属性来允许在后台做一些动作。
那么应用程序可以非常简单(我已经使用秒更容易编码),只需在main.js:
function checkTime() {
var sec = new Date().getSeconds();
switch(sec) {
case 0:
/// navigator.vibrate([ldot]); // EDIT: does not work in background support mode
// you can use Feedback API instead (hardcoded version of 3 vibrations)
// you can also experiment with other types of feedback events to find to most suitable
setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 0);
setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 300);
setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 900);
break;
case 15:
/// etc.
}
//check every second
setInterval(checkTime, 1000);
应用程序在后台也会引起设备震动。
- 如果你需要在UI和worker代码之间进行更多的划分,请检查'service application'的概念。但是请注意,服务应用程序对一些API的访问是有限的(例如navigator),所以你需要使用MessagePort API来告诉UI应用程序振动。一些伪代码:
service.js:
var port = tizen.messageport.requestRemoteMessagePort("znvYsIPC4D.WebUIServiceUI", "portService")
function checkTime() {
var sec = new Date().getSeconds();
var message;
switch(sec) {
case 15:
message = [{key: "type", value: "vibrate15"}]
break;
case 30:
message = [{key: "type", value: "vibrate30"}]
break;
case 45:
message = [{key: "type", value: "vibrate45"}]
break;
case 0:
message = [{key: "type", value: "vibrate0"}]
break;
}
message && port.sendMessage(message);
}
module.exports.onStart = function() {
console.log("onStart is called");
setInterval(checkTime, 1000);
};
module.exports.onStop = function() {
console.log("onStop is called");
};
main.js:
var port = tizen.messageport.requestLocalMessagePort("portService");
var listener = function(message) {
var type = message[0].value;
switch(type) {
case "vibrate0":
navigator.vibrate([ldot]);
break;
case "vibrate15":
// etc.
}
}
port.addMessagePortListener(listener);
// run a service
tizen.application.launch("znvYsIPC4D.Service", (s) => console.log("success"), (s) => console.log("error"))
- 最后,如果以上这些还不够,您可以考虑使用本地服务应用程序与Web UI应用程序作为混合应用程序,以获得更好的性能。
顺便说一句,电池使用率高的直接原因是一行:
tizen.power.request("CPU","CPU_AWAKE");
请删除它-后台支持配置可能会做你所期望的。