Android代碼----傳感器---方向傳感

jopen 11年前發布 | 22K 次閱讀 Android Android開發 移動開發

一個簡單的小Demo測試手機方向傳感:

具體代碼如下:

Android代碼----傳感器---方向傳感

[Java代碼]Sample.java

package com.example.test_sensor_orientation;

import org.openintents.sensorsimulator.hardware.Sensor;
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public  class Sample extends Activity implements  android.hardware.SensorEventListener {

    TextView myTextView1;
    TextView myTextView2;
    TextView myTextView3;

    private SensorManager mySensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myTextView1 = (TextView) findViewById(R.id.myTextView1);
        myTextView2 = (TextView) findViewById(R.id.myTextView2);
        myTextView3 = (TextView) findViewById(R.id.myTextView3);
        mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    }

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

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        mySensorManager.unregisterListener(this);
        super.onStop();
    }
    @Override
    protected void onPause() {
        mySensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(android.hardware.SensorEvent event) {
        // TODO Auto-generated method stub
        float[] values = event.values;
        int sensorType = event.sensor.TYPE_ORIENTATION;
        if (sensorType == Sensor.TYPE_ORIENTATION) {
            myTextView1.setText("Yaw為:"+values[0]); 
            myTextView2.setText("Pitch為:"+values[1]); 
            myTextView3.setText("Roll為:"+values[2]); 
        }
    }

}
[XML代碼]main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />
    <TextView
        android:id="@+id/myTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />
    <TextView
        android:id="@+id/myTextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

</LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello World, Sample</string> 
<string name="app_name">Sample</string> 
<string name="title">方向傳感器</string> 
<string name="myTextView1">Yaw為:</string> 
<string name="myTextView2">Pitch為:</string> 
<string name="myTextView3">Roll為:</string> 
</resources> 
來自:http://blog.csdn.net/jerome_xx/article/details/8711484

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!