2012/08/25

Android取得手機方向




Activity程式碼如下:
package com.scps;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

public class ORIENTATIONActivity extends Activity implements
        SensorEventListener {
    TextView textView = null;
    Sensor asensor = null;
    Sensor msensor = null;
    SensorManager manager = null;

    float[] accelerometerValues = new float[3];
    float[] magneticFieldValues = new float[3];
    float[] values = new float[3];
    float[] rotate = new float[9];
    private static final String TAG = "sensor";
    private static String str = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView1);
        manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        asensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        msensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 螢幕不滅

    }

    @Override
    protected void onPause() {
        super.onPause();
        manager.unregisterListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        manager.registerListener(this, asensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        manager.registerListener(this, msensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            accelerometerValues = event.values;
        }

        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            magneticFieldValues = event.values;
        }

        SensorManager.getRotationMatrix(rotate, null, accelerometerValues,
                magneticFieldValues);
        SensorManager.getOrientation(rotate, values); // 取得弧度
        values[0] = (float) Math.toDegrees(values[0]); // 將弧度轉化為角度
        textView.setText("角度=" + values[0]);
        String temp = "";
        if (values[0] >= -5 && values[0] < 5) {
            temp = "正北";
        } else if (values[0] >= 5 && values[0] < 85) {
            temp = "東北";
        } else if (values[0] >= 85 && values[0] <= 95) {
            temp = "正東";
        } else if (values[0] >= 95 && values[0] < 175) {
            temp = "東南";
        } else if ((values[0] >= 175 && values[0] <= 180)
                || (values[0]) >= -180 && values[0] < -175) {
            temp = "正南";
        } else if (values[0] >= -175 && values[0] < -95) {
            temp = "西南";
        } else if (values[0] >= -95 && values[0] < -85) {
            temp = "正西";
        } else if (values[0] >= -85 && values[0] < -5) {
            temp = "西北";
        }
        
        if (!str.equals(temp)) {
            str = temp;
            Log.i(TAG, str);
        }
    }
}

layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.17"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

參考文章:
http://blog.csdn.net/octobershiner/article/details/6641942