---
title: "对android重力测试的一个疑问"
author: "yinter"
date: "2010-01-04T11:29:20Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2010-01-04T11:50:35Z"
id: 561949
url: "https://yinter.iteye.com/blog/561949"
---

之前有看过IBM Eyes 关于SensorManager的代码，但因为没有手机，没法测试，今天把代码发布到手机上了(android版本为1.5)，终于可以知道他的坐标（X，Y，Z）的重力位置，代码如下：


```java

/*
 * 
 * IBMEyes.java
 * sample code for IBM Developerworks Article
 * Author: W. Frank Ableson
 * fableson@msiservices.com
 * 
 */

package com.msi.ibm.eyes;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class IBMEyes extends Activity implements SensorListener {
	
	final String tag = "IBMEyes";
	SensorManager sm = null;
	
	TextView xViewA = null;
	TextView yViewA = null;
	TextView zViewA = null;
	TextView xViewO = null;
	TextView yViewO = null;
	TextView zViewO = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
        xViewA = (TextView) findViewById(R.id.xbox);
        yViewA = (TextView) findViewById(R.id.ybox);
        zViewA = (TextView) findViewById(R.id.zbox);
        xViewO = (TextView) findViewById(R.id.xboxo);
        yViewO = (TextView) findViewById(R.id.yboxo);
        zViewO = (TextView) findViewById(R.id.zboxo);
        
    }
    
    
    
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this) {
            Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + 
            		", y: " + values[1] + ", z: " + values[2]);
            if (sensor == SensorManager.SENSOR_ORIENTATION) {
	            xViewO.setText("Orientation X: " + values[0]);
	            yViewO.setText("Orientation Y: " + values[1]);
	            zViewO.setText("Orientation Z: " + values[2]);
            }
            if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
	            xViewA.setText("Accel X: " + values[0]);
	            yViewA.setText("Accel Y: " + values[1]);
	            zViewA.setText("Accel Z: " + values[2]);
            }            
        }
    }
    
    public void onAccuracyChanged(int sensor, int accuracy) {
    	Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
        
    }
 

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this, 
                SensorManager.SENSOR_ORIENTATION |
        		SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    @Override
    protected void onStop() {
        sm.unregisterListener(this);
        super.onStop();
    }    
}

```


我把手机平放在桌面，屏幕朝上，（x，y，z）的值分别为（0，0，-10）；
手机翻过来放（与平放相反，即屏幕朝下），（x，y，z）的值分别为（0，0，10）；
手机竖着放，（x，y，z）的值分别为（0，-10，0）；
手机倒过来放（与竖相反），（x，y，z）的值分别为（0，10，0）；

说明一下，实际输出为float值，为了好确认方向，我取整了；

对这个值有个什么疑问呢，与[raymondlueng](http://rayleung.iteye.com/blog/467195)在博客写的正好相反，这就奇怪了。