我試圖獲得手機的傾斜角度,並且我的這項活動在我的LG Optimus S上完美工作運行ICS 4.0.2的Verizon Galaxy Nexus的TYPE_MAGNETIC_FIELD事件永遠不會觸發。TYPE_MAGNETIC_FIELD事件沒有在我的Galaxy Nexus上觸發ICS 4.0.2
因爲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 2012-05-14 09:00:06
我有同樣的問題。 – 2012-09-26 14:24:53
我們在Galaxy S3和Asus平板電腦上遇到同樣的問題。有人解決這個討厭的事情嗎? – WindRider 2014-09-23 10:32:25