Android Open Source - BallDemo Main Activity






From Project

Back to project page BallDemo.

License

The source code is released under:

GNU General Public License

If you think the Android project BallDemo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.ac.reading.sis05kol.mooc;
//from ww w . ja v a2 s .  com
import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final int MENU_RESUME = 1;
    private static final int MENU_START = 2;
    private static final int MENU_STOP = 3;

    private GameThread mGameThread;
    private GameView mGameView;  
  
  /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        setContentView(R.layout.activity_main);
        
        mGameView = (GameView)findViewById(R.id.gamearea);
        mGameView.setStatusView((TextView)findViewById(R.id.text));
        mGameView.setScoreView((TextView)findViewById(R.id.score));
                
        this.startGame(mGameView, null, savedInstanceState);
    }

    private void startGame(GameView gView, GameThread gThread, Bundle savedInstanceState) {      

      //Set up a new game, we don't care about previous states
      mGameThread = new TheGame(mGameView);
      mGameView.setThread(mGameThread);
      mGameThread.setState(GameThread.STATE_READY);
      mGameView.startSensor((SensorManager)getSystemService(Context.SENSOR_SERVICE));
    }

  /*
   * Activity state functions
   */
  
    @Override
    protected void onPause() {
        super.onPause();
        
        if(mGameThread.getMode() == GameThread.STATE_RUNNING) {
          mGameThread.setState(GameThread.STATE_PAUSE);
        }
    }

    
    @Override
  protected void onDestroy() {
    super.onDestroy();
      
      mGameView.cleanup();
        mGameView.removeSensor((SensorManager)getSystemService(Context.SENSOR_SERVICE));
        mGameThread = null;
        mGameView = null;
  }    
    
    /*
     * UI Functions
     */
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, MENU_START, 0, R.string.menu_start);
        menu.add(0, MENU_STOP, 0, R.string.menu_stop);
        menu.add(0, MENU_RESUME, 0, R.string.menu_resume);

        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_START:
                mGameThread.doStart();
                return true;
            case MENU_STOP:          
          mGameThread.setState(GameThread.STATE_LOSE,  getText(R.string.message_stopped));
                return true;
            case MENU_RESUME:
                mGameThread.unpause();
                return true;
        }

        return false;
    }
}




Java Source Code List

uk.ac.reading.sis05kol.mooc.GameThread.java
uk.ac.reading.sis05kol.mooc.GameView.java
uk.ac.reading.sis05kol.mooc.MainActivity.java
uk.ac.reading.sis05kol.mooc.TheGame.java