User Tools

Site Tools


built_in:service

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
built_in:service [2015/11/02 08:55]
octazid [Table] new methods
built_in:service [2018/12/29 16:47] (current)
Line 1: Line 1:
-====== Service control ======+====== Service ======
  
 ===== Methods ===== ===== Methods =====
Line 8: Line 8:
 | Service.SetOnMessage( callback )          |              | | Service.SetOnMessage( callback )          |              |
 | Service.Stop()                            |              | | Service.Stop()                            |              |
 +
 +===== Sample =====
 +//(posted by Dave Smart in the DroidScript Forum)//
 +
 +Create a new project called "Music Service" and copy the 2 files in the folder.
 +
 +**File 1:**
 +<code JavaScript Service.js>
 +//Called when service is started.
 +function OnStart()
 +{
 +app.ShowPopup( "Hello from Service!" );
 +
 +//Create media player.
 +player = app.CreateMediaPlayer();
 +player.SetOnReady( player_OnReady );
 +player.SetOnComplete( player_OnComplete );
 +
 +//Query media store for a nice long track.
 +media = app.CreateMediaStore();
 +media.SetOnMediaResult( media_OnMediaResult );
 +media.QueryMedia( "duration > 120000", "", "external" );
 +}
 +
 +//Handle media query results.
 +function media_OnMediaResult( result )
 +{
 +if( result.length==0 ) return;
 +
 +//Play first file found.
 +track = result[51];
 +player.SetFile( track.uri );
 +
 +//Get album art.
 +var img = app.CreateImage( null, 0.1, 0.1 );
 +var gotArt = media.GetAlbumArt( img, track.albumId, "external" );
 +
 +//Show a notification with album art if available.
 +notify = app.CreateNotification( "Ongoing" );
 +notify.SetMessage( track.title, track.title, track.album ); 
 +if( gotArt ) notify.SetLargeImage( img );
 +notify.Notify( "my_player_id" ); 
 +}
 +
 +//Called when we get a service message.
 +function OnMessage( msg )
 +{
 +//Show debug in WiFi IDE.
 +console.log( msg );
 +
 +//Handle commands from main App.
 +if( msg=="play" ) player.Play();
 +else if( msg=="pause" ) player.Pause();
 +else if( msg=="quit" ) notify.Cancel( "my_player_id" );
 +}
 +
 +//Called when player is ready to play.
 +function player_OnReady()
 +{
 +player.Play();
 +}
 +
 +//Called when playback has finished.
 +function player_OnComplete()
 +{
 +notify.SetMessage( "Track Finished!", track.title, track.album ); 
 +notify.Notify( "my_player_id" );
 +}
 +</code>
 +
 +
 +**File 2:**
 +<code JavaScript Music Service.js>
 +//Called when application is started.
 +function OnStart()
 +{
 +//Create a layout.
 +lay = app.CreateLayout( "linear", "VCenter,FillXY" ); 
 +
 +//Create a 'Play' button.
 +btn = app.CreateButton( "Play", 0.6, 0.1 );
 +btn.SetMargins( 0, 0.05, 0, 0 );
 +lay.AddChild( btn );
 +btn.SetOnTouch( btn_OnTouchPlay );
 +
 +//Create a 'Pause' button.
 +btn = app.CreateButton( "Pause", 0.6, 0.1 );
 +btn.SetMargins( 0, 0.05, 0, 0 );
 +lay.AddChild( btn );
 +btn.SetOnTouch( btn_OnTouchPause );
 +
 +//Create a 'Stop Service' button.
 +btn = app.CreateButton( "Stop Service", 0.6, 0.1 );
 +btn.SetMargins( 0, 0.05, 0, 0 );
 +lay.AddChild( btn );
 +btn.SetOnTouch( btn_OnTouchStop );
 +
 +//Add layout to app. 
 +app.AddLayout( lay );
 +
 +//Start/connect to our service.
 +svc = app.CreateService( "this","this", OnServiceReady );
 +
 +//This will cause your service to start at boot.
 +app.SetAutoBoot( "Service" );
 +}
 +
 +//Called after our service has started.
 +function OnServiceReady()
 +{
 +console.log( "Service Ready" );
 +}
 +
 +function btn_OnTouchStop()
 +{
 +//Tell service we are quitting.
 +svc.SendMessage( "quit" );
 +
 +//Stop the service.
 +svc.Stop();
 +}
 +
 +function btn_OnTouchPlay()
 +{
 +//Tell service to play music.
 +svc.SendMessage( "play" );
 +}
 +
 +function btn_OnTouchPause()
 +{
 +//Tell service to pause music.
 +svc.SendMessage( "pause" );
 +}
 +</code>
 +==== Links ====
 +There is another background service sample at [[sample_code:file_service|Web file service]]
built_in/service.1446454546.txt.gz ยท Last modified: 2015/11/02 16:55 (external edit)