Android 感應器,android感應器

來源:互聯網
上載者:User

Android 感應器,android感應器

1. 光照感應器

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"         /></RelativeLayout>

MainActivity.java

package liqing.lightsensortest;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.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView lightLevel;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lightLevel = (TextView) findViewById(R.id.light_level);        //擷取系統感應器的管理器        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 光照感應器        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);        // 光照感應器註冊(register)監聽,參數為(SensorEventListener執行個體,感應器對象,輸出資訊速率)        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);    }    private SensorEventListener listener = new SensorEventListener() {        @Override        public void onSensorChanged(SensorEvent event) {            float value = event.values[0];            lightLevel.setText("Current light level is " + value + " lx ");        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //登出監聽            sensorManager.unregisterListener(listener);        }    }}

2. 加速器感應器(搖一搖)

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        /></RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView lightLevel;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lightLevel = (TextView) findViewById(R.id.light_level);        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 accelerometer(加速器)感應器        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);    }    private SensorEventListener listener = new SensorEventListener() {        @Override        public void onSensorChanged(SensorEvent event) {            //方向感應器SensorEvent的取值有X軸,Y軸,Z軸,去絕對值Math.abs()            float x = Math.abs(event.values[0]);            float y = Math.abs(event.values[1]);            float z = Math.abs(event.values[2]);            if (x > 15 || y > 15 || z > 15) {                lightLevel.setText("搖一搖 ");            }        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //登出監聽            sensorManager.unregisterListener(listener);        }    }}

3.方向感應器(賽車遊戲控制方向) 使用加速感應器和地磁感應器,通過getRotationMatrix()和getOrientation()得到XYZ旋轉值

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/light_level"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"         /></RelativeLayout>

MainActivity.java

package liqing.lightsensortest;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.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private SensorManager sensorManager;    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.light_level);        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);        //得到 accelerometer(加速器)感應器        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        //帶到 magnetic_FIELD(地磁的_領域)感應器        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);        //注意(SENSOR_DELAY_GAME)感應器的速率改變的相當快        sensorManager.registerListener(listener, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);        sensorManager.registerListener(listener, magneticSensor, SensorManager.SENSOR_DELAY_GAME);    }    private SensorEventListener listener = new SensorEventListener() {        float[] accelerometerValues = new float[3];        float[] magneticValues = new float[3];        @Override        public void onSensorChanged(SensorEvent event) {            //注意SensorEvent是所以感應器的資訊(包括類型和值)            switch (event.sensor.getType()) {                case Sensor.TYPE_ACCELEROMETER:                    //clone()複製                    accelerometerValues = event.values.clone();                    break;                case Sensor.TYPE_MAGNETIC_FIELD:                    //clone()複製                    magneticValues = event.values.clone();                    break;                default:                    break;            }            float[] R = new float[9];//矩陣的值            float[] values = new float[3];//X,Y,Z旋轉的值            //getRotationMatrix(得到旋轉矩陣)            SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);            //getOrientation(得到選中的值)            SensorManager.getOrientation(R, values);            textView.setText(                    "X=" + values[0] + "\n"                     + "Y=" + values[1] + "\n"                     + "Z=" + values[2] + "\n");        }        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {        }    };    @Override    protected void onDestroy() {        super.onDestroy();        if (sensorManager != null) {            //登出監聽            sensorManager.unregisterListener(listener);        }    }}

聯繫我們

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