android - TYPE_MAGNETIC_FIELD 事件未在我的 Galaxy Nexus ICS 4.0.2 上触发

标签 android accelerometer android-sensors

我正在尝试获取手机的倾斜角度,并且我的这项 Activity 在我的 LG Optimus S 上完美运行,上面有 2.3,但在我运行 ICS 4.0.2 的库存 Verizon Galaxy Nexus 上,TYPE_MAGNETIC_FIELD 事件从不火灾。

因为 TYPE_MAGNETIC_FIELD 事件从不触发,所以我没有获得传递到 SensorManager.getRotationMatrix 所需的 mGravs 数组。所以我无法分辨手机的角度。正如我提到的,这段代码在我的其他设备上运行良好,我可以毫无问题地看到手机的倾斜角度。

有人知道为什么我不能用我的 Galaxy Nexus 参加这个 Activity 吗?是否对 SDK 进行了任何更改以阻止其工作?

这是我的 Activity :

package com.rain.united;

import android.app.Activity;
import android.content.pm.ActivityInfo;
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.widget.TextView;

public class Gameplay extends Activity implements SensorEventListener{
    private static final String TAG = "CameraDemo";
    private TextView mOrientationData;
    private SensorManager mSensMan; 
    private float mAzimuth; 
    private float[] mGravs = new float[3]; 
    private float[] mGeoMags = new float[3]; 
    private float[] mOrientation = new float[3]; 
    private float[] mRotationM = new float[9]; 
    private float[] mRemapedRotationM = new float[9]; 
    private boolean mFailed; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gameplay);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mOrientationData = (TextView) findViewById(R.id.orientation_data);

        mSensMan = (SensorManager) getSystemService(SENSOR_SERVICE); 
    }

    protected void onResume() {
        super.onResume();
        mSensMan.registerListener(this, mSensMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
                SensorManager.SENSOR_DELAY_UI); 
        mSensMan.registerListener(this, mSensMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                SensorManager.SENSOR_DELAY_UI); 
    }

    protected void onPause() {
        super.onPause();
        mSensMan.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) 
            return; 
        Log.d("united", "anything");
        switch (event.sensor.getType()) { 
            case Sensor.TYPE_MAGNETIC_FIELD:
                //I NEVER GET TO THIS CODE ON MY GALAXY NEXUS
                System.arraycopy(event.values, 0, mGravs, 0, 3); 
                Log.d("united", mGravs.toString());
                break;  
            case Sensor.TYPE_ACCELEROMETER: 
                // Here let's try another way: 
                for (int i=0;i<3;i++) mGeoMags[i] = event.values[i]; 
                    break; 
                default: 
                    return;
        } 
        if (SensorManager.getRotationMatrix(mRotationM, null, mGravs, mGeoMags)){ 
            //Rotate to the camera's line of view (Y axis along the camera's axis) 
            //TODO: Find how to use android.opengl.Matrix to rotate to an arbitrary coordinate system. 
            SensorManager.remapCoordinateSystem(mRotationM, SensorManager.AXIS_X, 
                    SensorManager.AXIS_Z, mRemapedRotationM); 
            SensorManager.getOrientation(mRemapedRotationM, mOrientation); 
            onSuccess(); 
        } else {
            onFailure();
        }
    }

    void onSuccess(){ 
        if (mFailed) mFailed = false; 
        //      Convert the azimuth to degrees in 0.5 degree resolution. 
        mAzimuth = (float) Math.round((Math.toDegrees(mOrientation[0])) *2)/ 2; 
        //      Adjust the range: 0 < range <= 360 (from: -180 < range <= 180). 
        mAzimuth = (mAzimuth+360)%360; // alternative: mAzimuth = mAzimuth>=0 ? mAzimuth : mAzimuth+360; 
        mOrientationData.setText("Azimuth= " + mAzimuth); 
    } 
    void onFailure() { 
        if (!mFailed) { 
            mFailed = true; 
            mOrientationData.setText("didn't get rotation info"); 
        } 
    }
}

最佳答案

    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) return; 

删除此行。磁传感器正在返回此精度状态,您的代码正在丢弃这些事件。

关于android - TYPE_MAGNETIC_FIELD 事件未在我的 Galaxy Nexus ICS 4.0.2 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487747/

相关文章:

android - 检测步行距离

Android 对多个 Dagger2 模块使用单个上下文

java - 在 Android 应用程序中将位图图像转换/保存为文件

java - 如何使用加速度计值显示数字

iphone - 使用 iPhone 加速度计模拟摩托车加速度

android - 在android中使用加速度计平滑移动图像

android - 使水平 ListView 的列表项的高度动态

android - 具有我自己的音频PCM轨道的多路视频

android - unity C# 设备运动方向捕捉

android - 动态添加数据行