【Android 筆記 五】 Android Sensor感應器介紹(一)重力感應加速度擷取

來源:互聯網
上載者:User

FETC項目指導老師提出了新的需求,想要在遊戲地圖中表現出使用者使用者當期移動的方向,再用GPS的話顯然很不靠譜,所以想到了android強大的感應器。。。

很多行動裝置都內建了感應器,android通過Sensor和SensorManager類抽象了這些感應器,通過這些類可以使用android裝置的感應器

一 介紹Sensor類

SDK只有一句介紹“Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.”,表示一個感應器的類,可以使用getSensorList方法(此方法屬於接下來要講的SensorManager)獲得所有可用的感應器,該方法返回的是一個List<Sensor>

下面的列表顯示了,Sensor所提供的所有服務
----------------------------------------------------------------------------------------------------------------------------------------------------------
Constants
int     TYPE_ACCELEROMETER     A constant describing an accelerometer sensor type. //三軸加速度感應器 返回三個座標軸的加速度  單位m/s2
int     TYPE_ALL     A constant describing all sensor types.                     //用於列出所有感應器
int     TYPE_GRAVITY     A constant describing a gravity sensor type.                //重力感應器
int     TYPE_GYROSCOPE     A constant describing a gyroscope sensor type            //陀螺儀 可判斷方向 返回三個座標軸上的角度
int     TYPE_LIGHT     A constant describing an light sensor type.                 //光線感應器 單位 lux 勒克斯
int     TYPE_LINEAR_ACCELERATION     A constant describing a linear acceleration sensor type.  //線性加速度
int     TYPE_MAGNETIC_FIELD     A constant describing a magnetic field sensor type.               //磁場感應 返回三個座標軸的數值  微特斯拉
int     TYPE_ORIENTATION     This constant is deprecated. use SensorManager.getOrientation() instead. //方向感應器 已淘汰 可以使用方法獲得
int     TYPE_PRESSURE     A constant describing a pressure sensor type                             //壓力感應器  單位 千帕斯卡
int     TYPE_PROXIMITY     A constant describing an proximity sensor type.                          //距離感應器
int     TYPE_ROTATION_VECTOR     A constant describing a rotation vector sensor type.             //翻轉感應器
int     TYPE_TEMPERATURE     A constant describing a temperature sensor type                  //溫度感應器 單位 攝氏度

----------------------------------------------------------------------------------------------------------------------------------------------------------
此類中包含的方法都是get型的 用來擷取所選sensor的一些屬性,sensor類一般不需要new而是通過SensorManager的方法獲得

二 介紹SensorManager類

SDK解釋:“SensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off. ”
SensorManager 允許你訪問裝置的感應器。通過傳入參數SENSOR_SERVICE參數調用Context.getSystemService方法可以獲得一個sensor的執行個體。永遠記得確保當你不需要的時候,特別是Activity暫訂時候,要關閉感應器。忽略這一點肯能導致幾個小時就耗盡電池,注意當螢幕關閉時,系統不會自動關閉感應器。

三 常用的感應器

(1) 加速度感應器

可以通過這個感應器獲得三個浮點型

x-axis
y-axis

z-axis

可參閱《android 進階編程2》中的一個插圖分析次資料

X Y Z分別對應values[0]到[2]

X表示左右移動的加速度

Y表示前後移動的加速度

Z表示垂直方向的加速度 (測試時發現,將手機置於水平案頭穩定後 XY均為0 Z的值為9.4 約等於重力加速度,依次可以做一個簡單的演算法實現重力測力計,有時間會給大家一個例子)

下面先看一個基本的擷取加速的demo,希望大家好好注意代碼中的注釋

/* * @author octobershiner * 2011 07 27 * SE.HIT * 一個示範android加速度感應器的例子 * */package uni.sensor;import java.util.Iterator;import java.util.List;import android.app.Activity;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.util.Log;public class SensorDemoActivity extends Activity {    /** Called when the activity is first created. *///設定LOG標籤private static final String TAG = "sensor";private  SensorManager sm;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //建立一個SensorManager來擷取系統的感應器服務        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);        //選取加速度感應器        int sensorType = Sensor.TYPE_ACCELEROMETER;                /*         * 最常用的一個方法 註冊事件         * 參數1 :SensorEventListener監聽器         * 參數2 :Sensor 一個服務可能有多個Sensor實現,此處調用getDefaultSensor擷取預設的Sensor         * 參數3 :模式 可選資料變化的重新整理頻率         * */        sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);            }        /*     * SensorEventListener介面的實現,需要實現兩個方法     * 方法1 onSensorChanged 當資料變化的時候被觸發調用     * 方法2 onAccuracyChanged 當獲得資料的精度發生變化的時候被調用,比如突然無法獲得資料時     * */    final SensorEventListener myAccelerometerListener = new SensorEventListener(){        //複寫onSensorChanged方法    public void onSensorChanged(SensorEvent sensorEvent){    if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){    Log.i(TAG,"onSensorChanged");        //圖解中已經解釋三個值的含義    float X_lateral = sensorEvent.values[0];    float Y_longitudinal = sensorEvent.values[1];    float Z_vertical = sensorEvent.values[2];    Log.i(TAG,"\n heading "+X_lateral);    Log.i(TAG,"\n pitch "+Y_longitudinal);    Log.i(TAG,"\n roll "+Z_vertical);    }    }    //複寫onAccuracyChanged方法    public void onAccuracyChanged(Sensor sensor , int accuracy){    Log.i(TAG, "onAccuracyChanged");    }    };        public void onPause(){    /*     * 很關鍵的部分:注意,說明文檔中提到,即使activity不可見的時候,感應器依然會繼續的工作,測試的時候可以發現,沒有正常的重新整理頻率     * 也會非常高,所以一定要在onPause方法中關閉觸發器,否則講耗費使用者大量電量,很不負責。     * */    sm.unregisterListener(myAccelerometerListener);    super.onPause();    }    }

測試的時候會發現重新整理的特別快,這就引出一個問題,如果真的要呈現在UI中的話,就會不斷的繪製介面,非常耗費資源,所以《android進階編程》中給出了一個方案就是引入新的線程來重新整理介面,明天有時間的話,盡量把把例子給大家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.