Android感應器的使用(GravieySensor)

來源:互聯網
上載者:User

標籤:

這裡以重力感應器為例說明

第一步:建立一個SensorManager對象,用來管理或者擷取感應器

SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);

第二步:根據Sensor下枚舉獲得對應的感應器對象

 Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);

第三步,為該感應器註冊一個事件,方法有很多種,可以讓Activi繼承SensorEventListener介面,或者直接使用內部類,推薦使用內部類:

 sm.registerListener(new SensorEventListener() {            @Override            public void onSensorChanged(SensorEvent event) {            }            @Override            public void onAccuracyChanged(Sensor sensor, int accuracy) {            }        }, sr, SensorManager.SENSOR_DELAY_NORMAL);

其中,第三個參數,SENSOR_DELAY_NORMAL表示感應器反應速度,這裡SENSOR_DELAY_GAME,SENSOR_DELAY_FAST等。

第四步:SensorEvent對象中的values[]數組中儲存了感應器具體數值,針對不同的感應器對象,values[]具有不同內容,長度也不一樣。對於Gravity感應器,這裡values有三個值,分別代表了其在x,y,z方向上的重力加速度,我們在xml檔案中簡單設定一下布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_gravity"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.xdwang.myapplication.Gravity"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/x"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/y"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/z"/></LinearLayout>

然後在onSensorChanged中對三個TXTVIEW設定值:

 sm.registerListener(new SensorEventListener() {            @Override            public void onSensorChanged(SensorEvent event) {                txtx.setText("x方向的重力加速度為:" + event.values[0] + "g/ms2");                txty.setText("y方向的重力加速度為:" + event.values[1] + "g/ms2");                txtz.setText("z方向的重力加速度為:" + event.values[2] + "g/ms2");            }            @Override            public void onAccuracyChanged(Sensor sensor, int accuracy) {            }        }, sr, SensorManager.SENSOR_DELAY_NORMAL);

這樣,我們就能即時得到重力感應器的資料了,具體怎麼使用這些資料,就要看你的用處了。

最後附上全部代碼:

import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class Gravity extends AppCompatActivity {    TextView txtx = null;    TextView txty = null;    TextView txtz = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_gravity);        SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);        Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);        txtx = (TextView) findViewById(R.id.x);        txty = (TextView) findViewById(R.id.y);        txtz = (TextView) findViewById(R.id.z);        sm.registerListener(new SensorEventListener() {            @Override            public void onSensorChanged(SensorEvent event) {                txtx.setText("x方向的重力加速度為:" + event.values[0] + "g/ms2");                txty.setText("y方向的重力加速度為:" + event.values[1] + "g/ms2");                txtz.setText("z方向的重力加速度為:" + event.values[2] + "g/ms2");            }            @Override            public void onAccuracyChanged(Sensor sensor, int accuracy) {            }        }, sr, SensorManager.SENSOR_DELAY_NORMAL);    }}

 

Android感應器的使用(GravieySensor)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.