See the sample that comes with DroidScript "Alarms -
Set alarms to wake up your App"
When the alarm fires, you can call your function, set the next alarm and exit the app.
A more professional method would be to use a background service but that will take a bit more research, especially if it is your first service.
//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a 'Set Repeat' button.
btnRepeat = app.CreateButton( "Set Repeat", 0.4, 0.1 );
btnRepeat.SetMargins( 0, 0.05, 0, 0 );
btnRepeat.SetOnTouch( btnRepeat_OnTouch );
lay.AddChild( btnRepeat );
//Create a 'Cancel Repeat' button.
btnCancel = app.CreateButton( "Cancel Repeat", 0.4, 0.1 );
btnCancel.SetMargins( 0, 0.05, 0, 0 );
btnCancel.SetOnTouch( btnCancel_OnTouch );
lay.AddChild( btnCancel );
//Add layout to app.
app.AddLayout( lay );
//Create media player and load file.
player = app.CreateMediaPlayer();
player.SetFile( "/Sys/Snd/Poing.ogg" );
}
//Called when alarm is triggered.
//(Even if your app is closed)
function OnAlarm( id )
{
app.ShowPopup( "Got Alarm: id = " + id );
player.SeekTo( 0 );
player.Play();
}
//Called when user touches our 'Set Repeating' button.
function btnRepeat_OnTouch()
{
//Get current time in milliseconds.
var now = new Date().getTime();
//Set alarm for 3 seconds time and repeat every day (1000*60*60*24)
app.SetAlarm( "Repeat", 4321, OnAlarm, now + 3000, 86400000 );
}
//Called when user touches our 'Cancel Repeat' button.
function btnCancel_OnTouch()
{
//Cancel the alarm with id 4321.
//(All alarms will be cancelled by a reboot)
app.SetAlarm( "Cancel", 4321 );
}
"Receive Intent
Receive intents from other apps"
//This example shows how to receive data from other apps
//like 'Tasker' which allow sending of Android Intents.
https://groups.google.com/d/msg/androidscript/Ozq8f-mc8R4/Fq5J8y18zggJ
I would do as much work as possible in Service.js to avoid having to display the main app.
I would code the service to set a new alarm (always using the same id) every time OnStart is called or OnAlarm is called.
In the OnStart function of Service.js, I recommend also adding
//////////
//Force service to foreground on newer devices (required).
if( app.GetBuildNum() > 25 )
app.SetInForeground( "Alarm set" );
//////////
This is how I calculate the alarm time for the next 8:30 pm
If you don't know the JavaScript Date object, it's quite helpful and you don't need to deal with things like month or year boundaries yourself.
{
// target time = next 8:30 pm
var tim = { hr: 20, min: 30, sec: 0, ms:0 };
var alrm = new Date();
var now = new Date();
alrm.setHours(tim.hr);
alrm.setMinutes(tim.min);
alrm.setSeconds(tim.sec);
alrm.setMilliseconds(tim.ms);
// check if 8:30pm is earlier today
if(now.getTime() > alrm.getTime())
alrm.setDate(alrm.getDate() + 1);
app.SetAlarm( "Set", 5432, OnAlarm, alrm );
}
As they change the rules with every new version, we are always playing catch-up.
Your battery question brings up a case in point.
If any app proceeds without user-input for a significant length of time, it leaves longer and longer pauses between checking for alarms, even if you have a separate alarm being repeated every five minutes to try to keep it awake.
You may find that with its special status, Tasker can keep further ahead of the curve than DroidScript can so I suggest keeping Tasker in mind as a last resort if this is for a personal project.
Obviously you don't want to ask other users to install Tasker if they don't already have it.
Keep us informed of how you get on.
If you have problems let us know. Ideally post runnable code that demonstrates any problem you are having if that is practical.
"Service.js" is a hard-coded filename and you're stuck with it.
you can call SetInForeground repeatedly with different info to change the display.
SendMessage from onServiceReady should work but if it doesn't seem to work, try wrapping it in setTimeout with a 100ms delay
notifications need an id for Android to use. It doesn't matter much what it is.
If I only want one notification from my app, I tend to use app.GetAppName() to generate the id, so it doesn't get muddled with other apps when testing in DroidScript.
The above was written before I looked at your spk.
Now I have looked at it, my main thought is that I'm probably not the person to help you.
Following what you are doing in testservicemain.js does not come naturally to me, even before any complexity is added.
The fifth parameter to SetInForeground is importance. Try setting it to "high"
app.SetInForeground(theAlarmHour + ':' + theAlarmMinutes,"Time "+lastStarted+" "+app.GetName(),"Img/TestService.png","Img/notify.png","high");