Android Life Cycle

The following example shows two important considerations in development for Android:

  • Life-cycle management - note the onPause() and onResume() methods.
  • Proper SymGym event handling - both for button presses via SgKeyMapper and limbs’ movements.
   package fit.symgym.android.examples;

   import android.app.Activity;
   import android.os.Bundle;
   import android.view.KeyEvent;

   import fit.symgym.android.api.SgStub;
   import fit.symgym.android.api.SgState;
   import fit.symgym.android.api.SgKeyMapper;
   import fit.symgym.android.api.SgWatcher;
   import fit.symgym.android.api.SgDispatcher;

   public class ExampleElaborate extends Activity {

       class ElaborateDispatcher implements SgDispatcher {
           public Activity activity = null;
           private SgKeyMapper mapper = null;

           public ElaborateDispatcher(Activity activity) {
               this.activity = activity;
               mapper = new SgKeyMapper(activity);
               mapper.mapLeftTrigger     (KeyEvent.KEYCODE_Z);
               mapper.mapLeftSubTrigger  (KeyEvent.KEYCODE_SPACE);
               mapper.mapLeftLeft        (KeyEvent.KEYCODE_UNKNOWN); // no keypress
           }

           public boolean dispatch(SgState st) {
               boolean handled = false;

               while(true) { // just for flow control...
                   if (Math.abs(st.getLeftArmVelocity()) > 5) {
                       mapper.keyPress(KeyEvent.KEYCODE_DPAD_LEFT);
                       handled = true;
                       break;
                   }

                   if (st.getRightLegVelocity() > 5 || st.getLeftLegVelocity() > 5) {
                       mapper.keyPress(KeyEvent.KEYCODE_DPAD_DOWN);
                       handled = true;
                       break;
                   }

                   break;
               } // while

               return handled || mapper.dispatchButtonPush(st);
           }
       }
       
       private SgStub sgStub = null;
       private SgWatcher sgWatcher = null;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           sgWatcher = new SgWatcher(new SgStub(this, "api_key_string_here"),
                                     new ElaborateDispatcher(this) );
       }
           
       @Override
       protected void onPause() {
       	super.onPause();
        sgWatcher.stop();
       }

       @Override
       protected void onResume() {
       	super.onResume();
        sgWatcher.start();
       }
   }