In a previous post I explained the differences between the code for the emulator and the code for the real device, but the switch from one to another and vice versa is very cumbersome.
In this post I propose a way to switch quickly from one situation to another with few changes.
The specific code for the emulator has moved to a class that extends Activity (SimulatorActivity), and similarly, the specific code for the real device is moved to a class that extends Activity (DeviceActivity); both classes implement SensorEventListener.
The specific code for your app is contained in a class that extends SimulatorActivity or DeviceActivity; you override the methods onCreate, onResume and onPause and their first line of code must be a call to super.onCreate, super.onResume and super.onPause.
To quickly switch between the code for the emulator to the code for the real device you can simply change what class extends and change the import.
Tag: sensor simulator
The Android developers can use Sensor Simulator to test their applications, in particular when they use sensors such as accelerometer or compass.
In this post I explain the differences in the code for an activity that runs on the emulator or a real device, and I don’t discuss how to install or launch the emulator, for those topics you can refer to Sensor Simulator.
Switching from the emulator to the real device and vice versa you need to make some code changes and these changes can be divided into 4 points.
- different imports
- connection to the simulator in the onCreate method (this part is missing in the current code for the device)
- different listeners in the onResume method
- different ways to get the type of sensor in the onSensorChanged method