It’s been almost a year since I got my first Android device (also it was the first Android powered phone ever released) - the HTC Dream (G1). I always wanted to write apps for Android, but never got to it, until today.

Setting up and Hello world

This part is easy and well documented. There are only a few things you need to do:

  1. Download the Android SDK and install it

  2. Download Eclipse

  3. Download and install Android development plugin for Eclipse

  4. Configure Eclipse to use Android SDK

  5. Update Android SDK configuration (download APIs and documentation)

There’s an official guide to help you do this.

It’s easy to create “Hello world” application, since every single new Android project does just that.

Designing the UI

It’s easy to do that too, because you need know next to none of programming. UI design is done in GUI, you just need to place items to their respective places. If you want to be uber hacker - you can always do this in pure XML or Java.

The UI of main activity is stored in /res/layouts/main.xml and is written in XML.

Functionality and the accelerometer

I’m not a Java programmer (yet) and know only the basics of Java, thus I had to do some extensive googling on how to get the accelerometer to work, because either the code didn’t work, either it was too complicated. Eventually I found some sample code on developer.android.com in reference, customized it a bit and used it. I’ll add the code below.

I was very surprised how sensitive the accelerometer was. It was able to measure down to 10^-6 (it might be even more precise). But I don’t need numbers that precise, so I rounded them to 10^-2.

Another thing that I found interesting was that the values were constantly changing. Not by much (only by ±0.1 m/s^2). Even though the phone was placed on the table and nothing seemed to be moving. Could it be because of my PC near it?

package com.tautvidas.accelerodroid;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.widget.EditText;
import java.math.*;

public class AcceleroDroid extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
protected EditText EditTextAxisX, EditTextAxisY, EditTextAxisZ;

/*public AcceleroDroid() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}*/

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("DEBUGTAG", "Creating default view");
EditTextAxisX = (EditText) findViewById(R.id.editTextXaxis);
EditTextAxisY = (EditText) findViewById(R.id.editTextYaxis);
EditTextAxisZ = (EditText) findViewById(R.id.editTextZaxis);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Log.d("DEBUGTAG", "Started listening to the accelerometer");
}

protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
Log.d("DEBUGTAG", "Stopped listening to the accelerometer");
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

public void onSensorChanged(SensorEvent event) {

EditTextAxisX.setText("" + Math.round(event.values[0] * 100.0) / 100.0);
EditTextAxisY.setText("" + Math.round(event.values[1] * 100.0) / 100.0);
EditTextAxisZ.setText("" + Math.round(event.values[2] * 100.0) / 100.0);
}
}

Debugging

This part was real fun. Using DDMS not only Eclipse got access to the app in a local Android Emulator, but also it was able to read Logcat logs from my phone, so any thrown exceptions or logs my app wrote appeared in Eclipse. I find it very cool.

How the app interprets data from the accelerometer

Well, take a look at the image on the right. I included the same image in the app itself.

Currently there is no way of downloading this, sorry