app.SetAlarm in Foreground Service & Samsung Always problem

298 views
Skip to first unread message

Mauritz Zondagh

unread,
Mar 20, 2019, 3:19:10 AM3/20/19
to DroidScript
Hi,
It seems that the app.SetAlarm with interval of 20 sec or more do not work in a foreground service with Samsung Always on. (Not sure if the Always on sleep mode has something to do with it or not)

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

Mauritz Zondagh

unread,
Mar 20, 2019, 3:25:51 AM3/20/19
to DroidScript
When i increase my interval, i decrease my static variable counter e.g. 6, I did not mention this in my previous post.
Thanks

Dave

unread,
Mar 22, 2019, 1:27:34 PM3/22/19
to DroidScript
Try using the 'exact' option in the SetAlarm() function and see if that helps.

Mauritz Zondagh

unread,
Mar 22, 2019, 4:45:35 PM3/22/19
to DroidScript
Hi Dave, thankyou for the response. Is this the correct syntax?

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

Steve Garman

unread,
Mar 22, 2019, 4:54:01 PM3/22/19
to DroidScript
You should try
app.SetAlarm("Set", 12, OnAlarm, nowAlarm + 60000,null,"Exact");

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.

Mauritz Zondagh

unread,
Mar 22, 2019, 5:06:34 PM3/22/19
to DroidScript
Thankyou Steve & Dave

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

Dave

unread,
Mar 23, 2019, 6:18:02 AM3/23/19
to DroidScript
Unfortunately Google keeps changing the way alarms work, and we keep needing to make changes to DS to cope with the changes.

The also say this:- "Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use."


It looks like there has been yet another change for API 23, so we might need to support setExactAndAllowWhileIdle method now too.

 

Mauritz Zondagh

unread,
Mar 23, 2019, 11:25:28 AM3/23/19
to DroidScript
Hi Dave

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

Mauritz Zondagh

unread,
Mar 25, 2019, 5:27:44 PM3/25/19
to DroidScript
I can confirm now that i only get 1 to max of 2 alarms of the expected 5 (60 sec intervals in 5 minutes) while in Samsung always on sleep mode.

So there is a problem in APK23 as Dave mentioned.

Mauritz Zondagh

unread,
Apr 8, 2019, 7:17:59 AM4/8/19
to DroidScript

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

Mauritz Zondagh

unread,
Apr 8, 2019, 7:38:51 AM4/8/19
to DroidScript
Just to validate my results. I get a alarm (vibrate) 60 seconds apart when phone is active. When i put the phone to Always on mode (without doing anything else), you get the first alarm maybe two alarms and thereafter randomly with minutes interval in between. If i make the phone active again all works well with 1 minute interval in between (but only when you open my app).

So something is definitely going to sleep.
My Android Pie settings is set to not put apps to sleep & my app is not listed under the sleeping apps.

A bit irritating to test, have to wait minutes doing nothing. If i set the alarm to lower interval time e.g. 20 sec (< 20) seconds it seems to work (Base on previous tests)

Thanks

Reply all
Reply to author
Forward
0 new messages