This shows you the differences between two versions of the page.
built_in:service [2018/12/29 08:47] administrator old revision restored (2015/12/17 17:08) |
built_in:service [2018/12/29 16:47] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Service ====== | ||
- | ===== Methods ===== | ||
- | ^ Method | ||
- | | Service.Send( cmd, | ||
- | | Service.SendImg( cmd,img ) | | | ||
- | | Service.SendMessage( msg ) | | | ||
- | | Service.SetOnMessage( callback ) | | | ||
- | | Service.Stop() | ||
- | |||
- | ===== Sample ===== | ||
- | //(posted by Dave Smart in the DroidScript Forum)// | ||
- | |||
- | Create a new project called "Music Service" | ||
- | |||
- | **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( " | ||
- | } | ||
- | |||
- | //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, | ||
- | |||
- | //Show a notification with album art if available. | ||
- | notify = app.CreateNotification( " | ||
- | notify.SetMessage( track.title, | ||
- | if( gotArt ) notify.SetLargeImage( img ); | ||
- | notify.Notify( " | ||
- | } | ||
- | |||
- | //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==" | ||
- | else if( msg==" | ||
- | else if( msg==" | ||
- | } | ||
- | |||
- | //Called when player is ready to play. | ||
- | function player_OnReady() | ||
- | { | ||
- | player.Play(); | ||
- | } | ||
- | |||
- | //Called when playback has finished. | ||
- | function player_OnComplete() | ||
- | { | ||
- | notify.SetMessage( "Track Finished!", | ||
- | notify.Notify( " | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | **File 2:** | ||
- | <code JavaScript Music Service.js> | ||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | //Create a layout. | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | //Create a ' | ||
- | btn = app.CreateButton( " | ||
- | btn.SetMargins( 0, 0.05, 0, 0 ); | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch( btn_OnTouchPlay ); | ||
- | |||
- | //Create a ' | ||
- | btn = app.CreateButton( " | ||
- | btn.SetMargins( 0, 0.05, 0, 0 ); | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch( btn_OnTouchPause ); | ||
- | |||
- | //Create a 'Stop Service' | ||
- | btn = app.CreateButton( "Stop Service", | ||
- | btn.SetMargins( 0, 0.05, 0, 0 ); | ||
- | lay.AddChild( btn ); | ||
- | btn.SetOnTouch( btn_OnTouchStop ); | ||
- | |||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | |||
- | // | ||
- | svc = app.CreateService( " | ||
- | |||
- | //This will cause your service to start at boot. | ||
- | app.SetAutoBoot( " | ||
- | } | ||
- | |||
- | //Called after our service has started. | ||
- | function OnServiceReady() | ||
- | { | ||
- | console.log( " | ||
- | } | ||
- | |||
- | function btn_OnTouchStop() | ||
- | { | ||
- | //Tell service we are quitting. | ||
- | svc.SendMessage( " | ||
- | |||
- | //Stop the service. | ||
- | svc.Stop(); | ||
- | } | ||
- | |||
- | function btn_OnTouchPlay() | ||
- | { | ||
- | //Tell service to play music. | ||
- | svc.SendMessage( " | ||
- | } | ||
- | |||
- | function btn_OnTouchPause() | ||
- | { | ||
- | //Tell service to pause music. | ||
- | svc.SendMessage( " | ||
- | } | ||
- | </ | ||
- | ==== Links ==== | ||
- | There is another background service sample at [[sample_code: |