Alarms

From WebOS101

Jump to: navigation, search

Example code that uses the alarm service to run a synchronization after some period of time. Note that you can set the delay period to fractions of a minute for testing purposes...

See also the section on Multi-Stage Applications for info on responding to the alarm service in your AppAssistant's handleLaunch method.

Note: If using the 'in' parameter for the alarm service the minimum (as of 1.4.5) is 5 minutes. If you use the 'at' parameter, you can set whatever time you want (in theory).

this.setSyncTimer = function(delayInMinutes){
Mojo.Log.info("Delay: ", delayInMinutes);
var d, mo, yr, hrs, mins, secs, myDateString, dStr, myDate;
Mojo.Log.info("Starting Sync");
dashInfo = {
title: MyAPP.appName + " Starting Sync!",
message: "Swipe to Cancel",
count: 1
};
 
//For testing purposes ONLY, set delay to 0.5 minutes!
//delayInMinutes = 0.5;
 
d = new Date();
d.setTime(d.getTime() + delayInMinutes * 60 * 1000);
mo = d.getUTCMonth() + 1;
if (mo < 10) {
mo = '0' + mo;
}
myDate = d.getUTCDate();
if (myDate < 10) {
myDate = '0' + myDate;
}
yr = d.getUTCFullYear();
//get hours according to GMT
hrs = d.getUTCHours();
if (hrs < 10) {
hrs = '0' + hrs;
}
mins = d.getUTCMinutes();
if (mins < 10) {
mins = '0' + mins;
}
secs = d.getUTCSeconds();
if (secs < 10) {
secs = '0' + secs;
}
myDateString = mo + "/" + date + "/" + yr + " " + hrs + ":" + mins + ":" + secs;
Mojo.Log.info("Date String", myDateString);
 
dStr = Mojo.Format.formatDate(d, 'medium');
Mojo.Log.info("Time is", dStr);
 
 
syncTimerId = new Mojo.Service.Request("palm://com.palm.power/timeout", {
method: 'set',
parameters: {
key: MyAPP.appId + '.sync',
//'in': '00:05:00',
at: myDateString,
wakeup: true,
uri: 'palm://com.palm.applicationManager/open',
params: {
'id': MyAPP.appId,
'params': {
action: 'sync',
}
}
},
onSuccess: function(){
Mojo.Log.info("Success in Setting up Sync!!! at", dStr);
}.bind(this)
});
};
 
this.clearSyncTimer = function(timerId){
if (timerId) {
new Mojo.Service.Request("palm://com.palm.power/timeout", {
method: "clear",
parameters: {
"key": MyAPP.appId + '.sync'
},
onSuccess: function(){
Mojo.Log.info("Cleared Sync Timer!");
}
});
}
};
}
Personal tools