【问题标题】:Replace android constructor with onCreate()用 onCreate() 替换 android 构造函数
【发布时间】:2014-12-28 22:23:39
【问题描述】:

我读到,你不应该使用构造函数。如何用 onCreate() 替换构造函数?

我需要 onCreate() 因为我喜欢用系统服务来实现振动

在 MainActivity 中

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

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();
}

【问题讨论】:

  • 我不明白你的问题。你能告诉我你在哪里读到你不能使用构造函数的参考吗? onCreate() 由系统在创建 Activity 时调用。您需要做的就是覆盖它并调用超类并在 onCreate() 中执行您想要执行的任何其他操作(如初始化、设置内容视图等)。希望对您有所帮助。
  • 另外,您不需要将 sensorManager 作为参数发送给 GameEngine 构造函数。您可以通过应用程序上下文的 getSystemService() 调用来访问它。

标签: android constructor oncreate vibration


【解决方案1】:

我现在解决了这个问题。我从 MainActivity.class 向 GameEngine.class 发送一个振动器,就像发送其他东西一样。

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

新的构造函数

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();
}

那我可以拨打vibrator.vibrate(200);

这是最简单的。

【讨论】:

    【解决方案2】:

    首先,由于您没有扩展任何内容,因此您无法将 onCreate() 作为 Android 框架中的生命周期方法使用。

    也许最好根据您的需要扩展 Activity 或更可能的 Service 并覆盖 onCreate() 并添加适当的代码结构,然后使用捆绑的游戏数据调用它并通过 Intent 传递。

    另外在主/UI 线程上进行任何计算工作可能会导致大量 Jank(丢帧),因此我会使用 IntentService/AsyncTask/Runnable 或其他任何繁重的工作。

    处理传感器的基本操作,例如:

    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;
    }
    }
    

    如果您有更多核心逻辑,我会在服务中运行它,无论是 IntentService 还是带有 AsyncTask、Runnable 或其他任何东西的服务。

    您可以使用各种方法、绑定服务(Messenger/IBinder)、广播(可能很慢)或通过 UI Looper 和处理程序回调 UI 以从 SensorEventListener 回调中处理传感器更改。

    【讨论】:

    • 要使用 messenger/ibinder,GameEngineSensor 服务应该实现 IBinder 并在 onBind() 中返回。但他返回null。此外,如果服务在不同的进程中运行,UI Looper/handler 将不起作用。我认为广播也不是非常适合游戏应用的方法。最好是使用有界服务并使用 bindService() 而不是 MainActivity 中的 startService()。
    • 请阅读我的笔记。我确实建议绑定服务而不是使用广播接收器。当我提供了一个基本的代码片段时,我从 IBinder 返回了 null。如果我要有效地做到这一点,那将有所不同。此外,我从初始代码中假设这不是他/她打算实现的 AIDL 绑定的高效绑定器实现。交付的代码旨在匹配初始查询的体验!
    • 好吧,OP 的问题是完全模棱两可的。因此,与其等待更多相关信息(作为评论从 OP 中询问),而是提供了此服务解决方案。我认为这很棒——不要误会我的意思。只是通过填补空白和突出警告来加强你的答案。和平。
    猜你喜欢
    • 2011-03-19
    • 2012-01-14
    • 2011-10-17
    • 2011-06-08
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多