1

I read, that you not should use the constructor. How can I replace the constructor with onCreate()?

I need onCreate() because I like to implement vibration with the system services

In the MainActivity

 gameEngine = new GameEngine((SensorManager)getSystemService(Context.SENSOR_SERVICE),gameView,this,this);

Constructor in the GameEngine

public GameEngine(SensorManager sensorManager, IGameView gameView, OnPlayerInHouseListener onPlayerInHouseListener, OnGameOverListener onGameOverListener) {
    this.sensorManager = sensorManager;
    this.gameView = gameView;
    this.onPlayerInHouseListener = onPlayerInHouseListener;
    this.onGameOverListener = onGameOverListener;
    this.gameView.clearObstacles();
}
Capaxi
  • 25
  • 6
  • i don't understand your question. can you please show me reference on where you read you can't use constructors? The onCreate() is called by the system when the Activity is created. All you need to do is override it and call the superclass and do whatever else (like initialization, setting content view, etc) you want to do in onCreate(). Hope that helps. – VJ Vélan Solutions Nov 01 '14 at 23:33
  • also, you don't need to send sensorManager as a parameter to GameEngine constructor. You can access it via the Application context's getSystemService() call. – VJ Vélan Solutions Nov 01 '14 at 23:35
  • [Here is the question on stackoverflow](http://stackoverflow.com/questions/3302177/android-activity-constructor-vs-oncreate) – Capaxi Nov 02 '14 at 08:35

2 Answers2

1

I solved the issue now. I send an vibrator from MainActivity.class to the GameEngine.class like I send the other things.

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);     
gameEngine = new GameEngine((SensorManager)getSystemService(Context.SENSOR_SERVICE),gameView,this,this,vibrator);

The new constructor

public GameEngine(SensorManager sensorManager, IGameView gameView, OnPlayerInHouseListener onPlayerInHouseListener, OnGameOverListener onGameOverListener, Vibrator vibrator) {
    this.sensorManager = sensorManager;
    this.gameView = gameView;
    this.onPlayerInHouseListener = onPlayerInHouseListener;
    this.onGameOverListener = onGameOverListener;
    this.vibrator = vibrator;
    this.gameView.clearObstacles();
}

then I can call vibrator.vibrate(200);

That was the easiest.

Capaxi
  • 25
  • 6
0

Firstly as you haven't extended anything onCreate() is not available to you as a lifecycle method within the Android framework.

Perhaps it may be best to extend Activity or more likely Service dependent on your needs and override onCreate() and add your appropriate code constructs and then call it with your game data bundled up and passed via an Intent.

Additionally doing any computational work on the main/UI thread will likely result in a lot of Jank (dropped frames) so I would IntentService/AsyncTask/Runnable or whatever the heavy lifting.

Something basic to handle the sensor like:

Activity.class

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

    Intent i = new Intent(this, GameEngineSensor.class);
    /* parcel up your game data here */
    startService(i);

GameEngine.class

public class GameEngineSensor extends Service implements SensorEventListener{


public GameEngineSensor() {
}


@Override
public void onCreate() {
    super.onCreate();

    /*unparcel game data here */

    SensorManager gameEngine = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

    //registerListener as appropriate..

}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
//....
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//....
}




@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

If you have more core logic I'd run it within a service, whether an IntentService or Service with AsyncTask, Runnable or whatever as a minimum.

You can callback the UI to handle the sensor changes from within the SensorEventListener Callbacks using various methods, bound service (Messenger/IBinder), Broadcast (likely slow), or via the UI Looper and a handler.

  • to use messenger/ibinder, the GameEngineSensor service should implement IBinder and return it in onBind(). But he is returning null. Also, UI Looper/handler won't work if the service is running in a different process. I think broadcasting is also not a very suitable method for gaming apps. Best is to use bounded service and use bindService() rather than startService() from MainActivity. – VJ Vélan Solutions Nov 02 '14 at 00:50
  • Please read my note. I did suggest Bound Services AND not to use a Broadcast Receiver. I returned null from IBinder as I provided a basic code fragment. If I were to do this efficiently it would be somewhat different. Also I am assuming from the initial code this isn't an AIDL bound highly efficient binder implementation he/she was intending to implement. The code delivered intended to match the experience of the initial query! –  Nov 02 '14 at 00:56
  • well, the OP question is completely ambiguous. so, instead of waiting for more relevant information (which was asked from the OP as a comment), this service solution was given. which i think is great - don't get me wrong. just strengthened your answer by filling in gaps and highlighting caveats. peace. – VJ Vélan Solutions Nov 02 '14 at 01:00