Hello World
- Methods

Services run in the background and can be used to monitor online databases, local communication ports or changes in data on the file system. They can also trigger notifications to the user and launch apps when changes or timeouts occur.

DroidScript Services run in a separate process and can take advantage of multi-CPU devices, this allows CPU intensive calculations or slow procedures to be done in the background without slowing down or interfering with the main application.

Any file in your project with the filename “Service.js” will run as a hidden background service.

A service can be created and started using the CreateService method of the app object in your main application file:.

srv = app.CreateService( packageName, className, callback, options ) → app object: Service

Use “this” for the packageName and className parameters.

Your service will start running when it's created and a foreground app must exist to manage the service and this foreground app must be run at least one time by the user.

You can set the service to start automatically when the device is booted if required; with the SetAutoBoot method of the app object.
app.SetAutoBoot( "Service" );

Stop a service using the Stop method.

Messages can be sent from your app to the service using the SendMessage method on the service object:
svc.SendMessage( msg );

You will need to provide a message handler to receive and process the messages (see example below).

Send messages from the service to the main app by using the SendMessage method of the app object:
app.SendMessage( msg );

This will also need a message handler on the application (see example below).

Adding services to your application is best done working with the WiFi editor (browser IDE) because you will need to edit more than one file and also see debug message from the service. Debug and error messages are shown in gray on the debug tab of the WiFi editor. Pressing the stop button in this IDE will stop both the service and the app, but pressing the back button on your device will stop the app but leave the service running.

Example - Services

//our Service.js code.
//In regular usage move this code to a separate Service.js file
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()
    //Create the Service.js file
    //(In regular usage create the file yourself)
    app.WriteFile("Service.js", servicejs );

    //Create a layout.
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );

    //Create text control to display data from the service.
    txt = app.CreateText( "", 0.4 );
    txt.SetTextSize( 22 );
    lay.AddChild( txt );

    //Create an 'Send Message' button.
    btn = app.CreateButton( "Send Message to Service", 0.6, 0.1 );
    lay.AddChild( btn );
    btn.SetOnTouch( function(){ svc.SendMessage("change"); } );

    //Create a 'Stop Service' button.
    btn = app.CreateButton( "Stop Service", 0.6, 0.1 );
    lay.AddChild( btn );
    btn.SetOnTouch( function(){ svc.Stop(); } );

    //Add layout to app.
    app.AddLayout( lay );

    //Start/connect to our service.
    svc = app.CreateService( "this", "this", OnServiceReady );
    svc.SetOnMessage( OnServiceMessage );

    //This will cause your service to start at boot.
    //(Set it to "none" if you need to stop it starting)
    //app.SetAutoBoot( "Service" );

//Called after our service has started.
function OnServiceReady()
    app.Debug( "Service Ready" );

//Called when messages comes from our service.
function OnServiceMessage( msg )
    txt.SetText( "Count: " + msg );
Copy All       Run      

Inspect the debug console and see the log messages from the service in gray.


The following methods are available on the Service object:

GetType() → string: “Service”
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: “this” or “<package>”
string: “this” or “<class>”
string: comma “,” separated: Persist
string: “none” or “min” or “low” or “high”
function( message )
Returns the control class name.
Sends a message to the service.
Run service in background.
Run service in foreground.
Define a callback function which is called when a message arrived from the service.
Stop the service.