I am running a service, with a Alarm set to call a procedure every x seconds. I would like the interval to be 1 minute. When x >= 20 sec, it seems that the callback procedure is called once only, or sometimes with minute delays in between (set for 1 minute). The results a bit random. However if i set the alarm to call every 10 sec, everything is working (with static var & counter checking > 6 to get 1 minute - but this uses more battery power).
If a set the alarm in the main app for a test only (not service) x >= 20 sec it is working , so only in the foreground service i am getting these problems.
Not sure if everything dies in the Samsung Always on sleep mode? (I am on Android Pie).
app.SetInterval do the same.
app.SetAlarm("Repeat"...) do not work.
In service.js
//var g_Interval;
function OnStart() {
var lg = "Img/" + app.GetAppName() + ".png";
if (!app.FileExists(lg)) lg = null;
var sm = "Img/notify.png";
if (!app.FileExists(sm)) sm = null;
var whereami = "Service " + (app.IsAPK() ? "APK" : "IDE");
app.SetInForeground("EVNotify", whereami, null, lg);
//app.SetPersist("true") Do not work!!!
var nowAlarm = new Date().getTime();
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 10000);
//app.SetAlarm("Repeat", 4321, OnAlarm, nowAlarm+3000,10000);
//g_Interval = setInterval(GetAppointments, (60 * 1000));
}
// Static Variable
function OnAlarm(id) {
if (typeof OnAlarm.No == 'undefined') OnAlarm.No = 0;
var nowAlarm = new Date().getTime();
if (OnAlarm.No >= 6) {
OnAlarm.No = 0
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 60000); // 10000 works well
GetAppointments()
} else {
OnAlarm.No++
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 60000); // 10000 works well
}
}
Anyone else having the same problem or have a solution?
Basically how do i set a accurate interval / timer in a service? Do not need to be accurate to < msec, but 1 or 3 second inaccuracy is ok. But not minutes!!
Your help is appreciated
Thanks
app.SetAlarm("Set exact", 12, OnAlarm, nowAlarm + 60000);
In Droidscript help, options is added at the end? (But no explanation of options, also no info in Google i could find). Anyway, it did not seem to work with small and capital e in exact.(at the end)
If i add it after Set, then i get alarms i would say more on time with active phone. Take some time to test in Always on sleep mode. Will get back later on this.
Thanks
Where the null is is the interval parameter, used if the alarm type is "Repeat".
The options are on the end. Capitalization makes no difference.
With the correct syntax, i can already see with a awake phone the callback is exact on the second after several minutes.
Will report the test result with Samsung always on sleep mode a.s.a.p., takes time to test my app.
Would be good to add this syntax in Droidscript help, i could not find any info. Maybe the sample in Droidscript update with the exact option. Why would someone want to use a in accurate alarm? So the exact option will probably be default for most users.
Thanks
I did a first test, all work 100% at exactly 60 sec interval for 5 minutes (per designed) while the phone is active. But when on Always on sleep mode, i only get the first of 5 alarms.
So it seems the problem is not solved. Will try a few more times.
But your post on API23 changes from Google is probably the issue. Will wait for new Droidscript release eith the proposed method (option). (I use the x version of Droidscript on my phone)
Thanks
So there is a problem in APK23 as Dave mentioned.
Hi Dave,
The new Alpha release 1735a5a SetAlarm option "- Added 'ExactIdle' option to app.SetAlarm()." does not seem to work in Samsung Always on Idle mode.
I do not get the 1 minute interval. Bit random, but more 2.5 minutes interval.
I call it first (in Service.js) function OnStart()
var nowAlarm = new Date().getTime();
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 10000,null,"ExactIdle");
and in function OnAlarm(id) i re-set the alarm
vibrate... (Added this for test)
var nowAlarm = new Date().getTime();
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 60000,null,"ExactIdle");
"Do my stuff" - I actually took this out for test
Thanks