(link fixed)

เพิ่มยานอวกาศ(Pyramid)
เรียกการใช้งานของเซนเซอร์วัดความลาดเอียง


(ภาพจาก emulator)

//ตัวอย่าง Code ที่เรียกใช้เซนเซอร์วัดความลาดเอียงของ Android
package com.peacedev.shooting;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class Sensors extends Activity implements SensorEventListener  {

	SensorManager sensorManager = null;

	public  float ox;
	public  float oy;
	public  float oz;

	public  float ax;
	public  float ay;
	public  float az;

	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
	 }

	 @Override
	 protected void onResume() {
	    super.onResume();
	    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
	    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME);
	 }

	public void onAccuracyChanged(android.hardware.Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		  switch (event.sensor.getType()){
          case Sensor.TYPE_ACCELEROMETER:
        	  ax = event.values[0];
        	  ay = event.values[1];
        	  az = event.values[2];
          break;
      case Sensor.TYPE_ORIENTATION:
    	  ox = event.values[0];
    	  oy = event.values[1];
    	  oz = event.values[2];
      break;
		  }
	}

	@Override
	 protected void onStop() {
	    super.onStop();
	    sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
	    sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
	 }

}

download link (.apk 37KB)
http://dl.dropbox.com/u/30815442/android/Shooting%20256%20d3r.apk

Advertisements