我试图使用加速度计和磁场传感器在我的Nexus 4,但它不工作,我使用以下代码.
protected Sensor compass;
protected Sensor accelerometer;
protected SensorManager sensorManager;
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
compass = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
try {
sensorManager.registerListener(this, compass, SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
Log.d(TAG, "Registered CompassSensorWatcher");
} catch (Exception e) {
Log.e("could not register listener", e.toString());
public void onSensorChanged(SensorEvent event) {
// Logger.d("sensor changed "+event);
// we use TYPE_MAGNETIC_FIELD to get changes in the direction, but use SensorManager to get directions
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
return;
// Gets the value of the sensor that has been changed
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
Log.d(TAG, "Accelerometer onSensorChanged() ");
Log.d(TAG,String.valueOf(gravity[0]) );
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomag = event.values.clone();
break;
}
}我能很好地得到磁传感器值..。但不是加速度计值..。
我在多部手机上试过这个..。但这些问题似乎只出现在我的Nexus 4上!我在我的三星S4上试了一下,它的效果很好。但不是在Nexus 4..。
Nexus 4使用Android4.3。
然后我升级到Android4.4.2,但是加速度计仍然不能工作。
我试过使用其他几个使用加速计(SensorBox)的商店应用程序。它在Nexus 4上运行得很好,但我的代码不行.
我还把它放在我的AndroidManifest.xml中:
<uses-feature android:name="android.hardware.sensor.accelerometer" />
<uses-feature android:name="android.hardware.sensor.compass" />但问题似乎只有尼克斯4号..。
我竟然能把磁传感器用得很好。但不是加速度计!有人能提点建议吗??
发布于 2014-02-19 20:35:17
好的,在我的手机闪动超过5次后,..I找到了解决方案。这个问题是一个已知的问题:http://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=62876
https://productforums.google.com/forum/#!topic/mobile/b0duELH-tV0
通过将我的Nexus降级到4.2.2来解决这个问题。
下面是降级说明:http://www.cultofandroid.com/46632/downgrade-nexus-4-to-android-4-3-jelly-bean-from-kitkat/2/ (您可以对4.2.2遵循相同的说明)
我试过Android 4.3,4.4.2。CyanogenMod 4.4.2 .传感器事件对加速度计的要求只是不起作用..。
一旦我降级到4.2.2 .它开始运转良好..。
似乎有些bug/驱动程序问题,谷歌还没有修复!
https://stackoverflow.com/questions/21868803
复制相似问题