Categories
Android

The differences between the code for the emulator and the code for a real device

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.

  1. different imports
  2. connection to the simulator in the onCreate method (this part is missing in the current code for the device)
  3. different listeners in the onResume method
  4. different ways to get the type of sensor in the onSensorChanged method


Here below I write the code with which you can connect to the emulator, to switch to the code for the real device comment the lines between
/ / SENSOR SIMULATOR
and
/ / **********
and uncomment the lines between
/ / REAL DEVICE
and
/ / **********.

If you don’t want to use AsyncTask to connect to emulator, you can replace

with

To get other info you can see this post.

The line

may also be necessary for the emulator, for example because you have to get the rotation matrix (getOrientationMatrix method) or spatial orientation (method getOrientation).

One reply on “The differences between the code for the emulator and the code for a real device”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.