Back

Services

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:

 svc = app.CreateService( packageName, className, callback );

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 on the main app object.

 app.SetAutoBoot( "Service" );

Stop a service using the Stop method.

 scv.Stop();

Messages can be sent from your app to the service using the SendMessage method on the service object:

 scv.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.

Background Service.js


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

    //Create text control to display data from the service.
    txt = app.CreateText( "", 0.4 );
    txt.SetMargins( 0, 0.05, 0, 0 );
    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()
  {
    console.log( "Service Ready" );
  }

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

Copy All

Create a new file called Service.js in your application folder and copy the following code into it:

Service.js


    //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, 1000 );
    }

    //Called when we get a message from main app.
    function OnMessage( msg )
    {
        console.log( msg );

        //Handle commands from main App.
        if( msg=="change" ) diff = (diff > 0 ? -1 : 1);
    }

    //Do some work.
    function DoWork()
    {
        //This is where we do some regular background task
        //(here we just modify a counter).
        count += diff;

        //Send data to the App (if it is running).
        app.SendMessage( count );
    }

Copy All

Run the application and see the count increment every second. Click the Send Message to Service button and see the cound direction change.

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

See 'CreateService for more informations and a complete function list