Android1.5のAPI変更点「Android 1.5 Version Notes | Android Developers」の一つとして、
Redesigned Sensor Manager APIs
と書かれているとおり、Sensor ManagerのAPIが変更になりました。
パッケージ(Package)
1.1と1.5ともに、センサーAPIは、android.hardwareパッケージにあり、変更なし。
インターフェース(Interface)
1.1では、SensorListenerだけですが、
1.5では、SensorListenerが非推奨APIとなり、その置き換えとしてSensorEventListenerが追加されました。
SensorListener、SensorEventListenerインターフェースともに、実装すべきメソッドは、
onAccuracyChanged()とonSensorChanged()の2つですが、引数が変更になっています。
クラス(Class)
1.1では、SensorManagerだけですが、
1.5では、SensorManagerに加え、GeomagneticField、Sensor、SensorEventが追加されています。
SensorManagerについては、メソッドの変更が加えられており、
必ず使うところでは、registerListener()の引数が、
1.1では、registerListener(SensorListener, int)だけですが、
1.5では、registerListener(SensorEventListener, Sensor, int)が追加され、従来のものが非推奨APIになっています。
Sensorは、SensorManager.getSensorList()で取得でき、
従来、利用可能なセンサをビットフィールドでまとめて表現していたものを個別で表現できるように置き換えたものです。
SensorEventは、SensorListenerの置き換えであるSensorEventListenerの引数として使われるものです。
サンプルソースコード
現時点(2009/4/30)では、Android SDK 1.5のサンプルソースコードに含まれるApiDemos - Sensors.javaは、
1.1から更新されておらず、1.5用のコードはありませんでした。。。
よって、以下に1.1と1.5の違いを比較できるようなサンプルソースコードを作成しましたので掲載しておきます。
「// 1.1」と「// 1.5」 の箇所を入れ替えることで、両方試すことができます。
package com.adakoda.sensortest;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor; // 1.5
import android.hardware.SensorEvent; // 1.5
import android.hardware.SensorEventListener; // 1.5
//import android.hardware.SensorListener; // 1.1
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class SensorTestActivity extends Activity
implements SensorEventListener { // 1.5
// implements SensorListener { // 1.1
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegisteredSensor = false;
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// 1.5
{
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() > 0) {
Sensor sensor = sensors.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
// // 1.1
// {
// int sensors = mSensorManager.getSensors();
// if ((sensors | SensorManager.SENSOR_ORIENTATION) != 0) {
// mRegisteredSensor = mSensorManager.registerListener(this,
// SensorManager.SENSOR_ORIENTATION,
// SensorManager.SENSOR_DELAY_FASTEST);
// }
// }
}
@Override
protected void onPause() {
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { // 1.5
// public void onAccuracyChanged(int sensor, int accuracy) { // 1.1
}
@Override
public void onSensorChanged(SensorEvent event) { // 1.5
// public void onSensorChanged(int sensor, float[] values) { // 1.1
// 1.5
{
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
// values[0]:
// Azimuth, angle between the magnetic north direction and the Y axis,
// around the Z axis (0 to 359). 0=North, 90=East, 180=South, 270=West
// values[1]:
// Pitch, rotation around X axis (-180 to 180),
// with positive values when the z-axis moves toward the y-axis.
// values[2]:
// Roll, rotation around Y axis (-90 to 90),
// with positive values when the x-axis moves away from the z-axis.
Log.v("ORIENTATION",
String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]));
}
}
// // 1.1
// {
// if (sensor == SensorManager.SENSOR_ORIENTATION) {
// // values[0]:
// // Azimuth, rotation around the Z axis (0<=azimuth<360).
// // 0 = North, 90 = East, 180 = South, 270 = West
// // values[1]:
// // Pitch, rotation around X axis (-180<=pitch<=180),
// // with positive values when the z-axis moves toward the y-axis.
// // values[2]:
// // Roll, rotation around Y axis (-90<=roll<=90),
// // with positive values when the z-axis moves toward the x-axis.
// Log.v("ORIENTATION",
// String.valueOf(values[0]) + ", " +
// String.valueOf(values[1]) + ", " +
// String.valueOf(values[2]));
// }
// }
}
}