Android SDK 1.5 以後取得 Sensor 的方法
下列CODE是我自己實際在用的
相較大部分的範例都是1.1的
這一個方法特別的實用
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor; // 1.5
import android.hardware.SensorEvent; // 1.5
import android.hardware.SensorEventListener; // 1.5
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorTest extends Activity implements SensorEventListener {
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
TextView textview = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor);
textview = (TextView) findViewById(R.id.textSensor);
textview.setText("TEST");
mRegisteredSensor = false;
mSensorManager =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
/*覆寫onResuem方法*/
@Override
protected void onResume() {
super.onResume();
// 1.5
//{
List sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() > 0) {
Sensor sensor = sensors.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
/*覆寫onPause方法*/
@Override
protected void onPause()
{
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { // 1.5
}
@Override
public void onSensorChanged(SensorEvent event) { // 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]));
textview.setText(
String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]));
}
}
}
}
相較大部分的範例都是1.1的
這一個方法特別的實用
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor; // 1.5
import android.hardware.SensorEvent; // 1.5
import android.hardware.SensorEventListener; // 1.5
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorTest extends Activity implements SensorEventListener {
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
TextView textview = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor);
textview = (TextView) findViewById(R.id.textSensor);
textview.setText("TEST");
mRegisteredSensor = false;
mSensorManager =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
/*覆寫onResuem方法*/
@Override
protected void onResume() {
super.onResume();
// 1.5
//{
List
if (sensors.size() > 0) {
Sensor sensor = sensors.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
/*覆寫onPause方法*/
@Override
protected void onPause()
{
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { // 1.5
}
@Override
public void onSensorChanged(SensorEvent event) { // 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]));
textview.setText(
String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]));
}
}
}
}
留言