Send a message from a Service to the main app.
Example - Services
var servicejs = `
//Init variables.
var count = 0;
var diff = 1;
//Called when service is started.
function OnStart()
{
app.ShowPopup( "Hello from Service!" );
//Start a timer to do some regular work.
setInterval( DoWork, 500 );
}
//Called when we get a message from main app.
function OnMessage( msg )
{
app.Debug( msg );
//Handle commands from main App.
if( msg == "change" ) diff = (diff > 0 ? -1 : 1);
}
//This is where we do some regular background task
//(here we just modify a counter and send it back to the app, if its running).
function DoWork()
{
count += diff;
app.SendMessage( count );
}`
function OnStart()
{
app.WriteFile("Service.js", servicejs );
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
txt = app.CreateText( "", 0.4 );
txt.SetTextSize( 22 );
lay.AddChild( txt );
btn = app.CreateButton( "Send Message to Service", 0.6, 0.1 );
lay.AddChild( btn );
btn.SetOnTouch( function(){ svc.SendMessage("change"); } );
btn = app.CreateButton( "Stop Service", 0.6, 0.1 );
lay.AddChild( btn );
btn.SetOnTouch( function(){ svc.Stop(); } );
app.AddLayout( lay );
svc = app.CreateService( "this", "this", OnServiceReady );
svc.SetOnMessage( OnServiceMessage );
}
function OnServiceReady()
{
app.Debug( "Service Ready" );
}
function OnServiceMessage( msg )
{
txt.SetText( "Count: " + msg );
}