我的Galaxy Nexus ICS 4.0.2无法触发TYPE_MAGNETIC_FIELD事件

5
我试图获取手机的倾斜角度。我有一个在LG Optimus S上运行完美的活动,该设备搭载2.3版本。但是,在我的Verizon Galaxy Nexus股票上运行ICS 4.0.2时,TYPE_MAGNETIC_FIELD事件从不触发。
因为TYPE_MAGNETIC_FIELD事件从未触发,所以我无法获得mGravs数组,也就无法将其传递到SensorManager.getRotationMatrix中。因此,我无法确定手机的角度。正如我提到的,这段代码在我的另一台设备上运行良好,我可以毫无问题地看到手机的倾斜角度。
有人知道为什么我的Galaxy Nexus不会收到此事件吗?SDK是否有任何更改会阻止此功能工作?
以下是我的活动:
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"); 
        } 
    }
}

你处理好了吗? - Mikooos
我们在Galaxy S3和Asus平板电脑上遇到了同样的问题。有人修复这个麻烦事了吗? - WindRider
1个回答

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

请删除此行。磁传感器正在返回此准确性状态,而您的代码正在丢弃这些事件。


谢谢,它对SE Xperia Mini X8和三星Galaxy S1(以及可能还有许多其他设备)都有帮助。 - Hrk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接