User Tools

Site Tools


built_in:app_events

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:app_events [2014/10/13 16:19]
stevegarman
built_in:app_events [2018/12/30 13:31] (current)
Line 3: Line 3:
  
 They are They are
-^Function ^Called when ^ +^ Function        ^ Called when                                                                 
-|OnStart()|application is created | +| OnStart()       | application is created                                                      
-|OnMenu( name ) |user selects a menu item | +| OnMenu( name )  | user selects a menu item                                                    
-|OnBack() |the back key is pressed but only if it follows app.EnableBackKey( false ); | +| OnBack()        | the back key is pressed but only if it follows app.EnableBackKey( false );  
-|OnPause()|application is paused. (eg. When user switches to home screen)| +| OnPause()       | application is paused. (eg. When user switches to home screen)              
-|OnResume() |application is resumed. (eg. When user returns from home screen) | +| OnResume()      | application is resumed. (eg. When user returns from home screen)            
-|OnConfig() |configuration changes. (eg. When user rotates phone) | +| OnConfig()      | configuration changes. (eg. When user rotates phone)                        
-|OnAlarm() |an alarm previously set by app.SetAlarm() is triggered |+| OnAlarm()       | an alarm previously set by app.SetAlarm() is triggered                      | 
 +| OnData()        | data sent by Android Intents                                                |
  
 The code of the sample that is bundled with DroidScript is reproduced below without permission. Hope you don't mind, guys. The code of the sample that is bundled with DroidScript is reproduced below without permission. Hope you don't mind, guys.
Line 20: Line 21:
  app.SetMenu( "MyMenu1,MyMenu2" );  app.SetMenu( "MyMenu1,MyMenu2" );
  app.EnableBackKey( false );  app.EnableBackKey( false );
-}+ 
 +        //Get current time in milliseconds.  
 +        var now = new Date().getTime();  
 +        //Set alarm for 3 seconds time.  
 +        app.SetAlarm( "Set", 1234, OnAlarm, now + 3000 ); }
  
 //Called when user selects a menu item. //Called when user selects a menu item.
Line 54: Line 59:
 {               {              
  app.ShowPopup( "OnConfig" );      app.ShowPopup( "OnConfig" );    
 +}
 +
 +//Called when alarm is triggered. 
 +//(Even if your app is closed) 
 +function OnAlarm( id ) 
 +
 +    app.ShowPopup( "Got Alarm: id = " + id ); 
 +
 +
 +//Handle data sent from other apps.
 +function OnData( isStartUp )
 +{
 +    //Display intent data.
 + var intent = app.GetIntent();
 + if( intent )
 + {
 +            //Extract main data.
 +            var s = "action: " + intent.action + "\n";
 +            s += "type: " + intent.type + "\n";
 +            s += "data: " + intent.data + "\n\n";
 +        
 +            //Extract extras.
 +            s += "extras:\n";
 +            for( var key in intent.extras ) 
 +                s += key+": "+intent.extras[key] + "\n";
 +        
 +            app.Alert( s, "OnData" ); 
 + }
 +}
 +
 +</code>
 +
 +=====OnConfig=====
 +The following code can be useful if you want to change the layout in the OnConfig event.
 +
 +<code javascript>
 +//Called when application is started.
 +function OnStart()
 +{
 +  //Create a layout with objects vertically centered.
 +  lay = app.CreateLayout( "linear", "VCenter,FillXY" );  
 +  var test = app.GetOrientation();
 +  var hv = "Vertical";
 +  if( test=="Landscape") hv= "Horizontal";
 +  lay.SetOrientation( hv );
 + 
 +  txt = app.CreateText( "Hello " );
 +  txt.SetTextSize( 64);
 +  lay.AddChild( txt );
 + 
 +  txt1 = app.CreateText( "Goodbye" );
 +  txt1.SetTextSize( 64 );
 +  lay.AddChild( txt1 );
 +  app.AddLayout( lay );
 +}
 +//Called when screen rotates
 +function OnConfig()
 +{
 +  var test = app.GetOrientation();
 +  var hv = "Vertical";
 +  if( test=="Landscape") hv= "Horizontal";
 +  lay.SetOrientation( hv );
 } }
 </code> </code>
built_in/app_events.1413217141.txt.gz · Last modified: 2014/10/14 00:19 (external edit)