Android 中的移動感知功能

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

專案下載:Sensor1.zip

執行結果

本範例必須在手機上執行,每 0.5 秒會顯示一次 (TYPE_ACCELEROMETER) ax, ay, ax 與 (TYPE_ORIENTATION) ox, oy, oz。在模擬器上由於沒有移動感知器,因此會顯示下列圖形。

Sensor1Run.png

其中的 ox, oy, oz 的意義如下

the values have to be recalculated to become a correct 3d position: 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

程式範例:Sensor1.java

package ccc.android;
 
import java.util.Date;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
 
public class Sensor1 extends Activity {
    SensorObj mSensor = null;
    Panel mPanel = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensor = new SensorObj() {
            long time = new Date().getTime();
            public void onSensorChanged(SensorEvent event) {
                long now = new Date().getTime();
                if (now - time > 500)
                synchronized (this) {
                    setData(event);
                    mPanel.invalidate();
                    time = now; 
                }
            }            
        };        
        mPanel = new Panel(Sensor1.this);
        setContentView(mPanel);
    }
 
    @Override
    protected void onResume() {
       super.onResume();
       mSensor.register();
    }
 
    @Override
    protected void onStop() {
       super.onStop();
       mSensor.unregister();
    }    
 
    class Panel extends View {
        public Panel(Context context) {
            super(context);
        }
 
        public void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setColor(Color.WHITE);
            SensorObj s = Sensor1.this.mSensor;
            canvas.drawText("ax:"+s.ax, 1, 20, paint);
            canvas.drawText("ay:"+s.ay, 1, 40, paint);
            canvas.drawText("az:"+s.az, 1, 60, paint);
            canvas.drawText("ox:"+s.ox, 1, 80, paint);
            canvas.drawText("oy:"+s.oy, 1, 100, paint);
            canvas.drawText("oz:"+s.oz, 1, 120, paint);
        }        
    }
 
    class SensorObj implements SensorEventListener {
        SensorManager sensorManager = null;        
        float ax, ay, az;
        float ox, oy, oz;
 
        public SensorObj() {
            sensorManager = (SensorManager) 
              getSystemService(SENSOR_SERVICE);            
        }
 
        public void setData(SensorEvent event) {
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    this.ax = event.values[0];
                    this.ay = event.values[1];
                    this.az = event.values[2];
                    break;
                case Sensor.TYPE_ORIENTATION:
                    this.ox = event.values[0];
                    this.oy = event.values[1];
                    this.oz = event.values[2];
                    break;
            }
        }
 
        public void register() {
              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 unregister() {
            sensorManager.unregisterListener(this, 
              sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
            sensorManager.unregisterListener(this, 
              sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
         }
 
        public void onSensorChanged(SensorEvent event) {}
 
        public void onAccuracyChanged(Sensor sensor, int accuracy) {}        
    }    
}

參考文獻

  1. Using an Android phone's sensors, Fri, 04/30/2010 - 06:23 | by tamas
  2. Need to calculate rotation-vector from Sensor.TYPE_ORIENTATION data

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License