达累斯萨拉姆。我在我的三星设备中从我的陀螺仪中获取数据,但在lenovo a3000中什么也没得到, 在三星,我在我的文本视图前轮换,但在联想我没有任何东西,甚至是零。它是空的。 我应该如何阅读Lenovo A3000中的旋转(滚动,俯仰,航向)? 我的代码如下:
public class CalculateDataService extends Service{
final static String TAG = "Roshan java";
SensorManager sensorManager;
private int currentDataIndex = 0;
int orientationSensor;
float headingAngle;
float pitchAngle;
float rollAngle;
LocationManager locationManager;
double latitude;
double longitude;
double altitude;
@Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
orientationSensor = Sensor.TYPE_ORIENTATION;
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_GAME);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
Log.d(TAG, "Latitude: " + String.valueOf(latitude));
Log.d(TAG, "Longitude: " + String.valueOf(longitude));
Log.d(TAG, "Altitude: " + String.valueOf(altitude));
}
public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
};
final SensorEventListener sensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
headingAngle = sensorEvent.values[0];
pitchAngle = sensorEvent.values[1];
rollAngle = sensorEvent.values[2];
Log.d(TAG, "Heading: " + String.valueOf(headingAngle));
Log.d(TAG, "Pitch: " + String.valueOf(pitchAngle));
Log.d(TAG, "Roll: " + String.valueOf(rollAngle));
try {
File myFile = new File("/sdcard/roshan");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(
currentDataIndex+","+
makePrecision2(rollAngle)+","+
makePrecision2(pitchAngle)+","+
makePrecision2(headingAngle)+","+
latitude+","+
longitude+","+
altitude
);
currentDataIndex++;
myOutWriter.close();
fOut.close();
} catch (Exception e) {
Log.v(TAG, e.getMessage());
}
}
}
public void onAccuracyChanged (Sensor senor, int accuracy) {
//Not used
}
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
locationManager.removeUpdates(locationListener);
sensorManager.unregisterListener(sensorEventListener);
super.onDestroy();
}
private float makePrecision2(float input){
return ((float)((int)(input*100f)))/100f;
}
}
答案 0 :(得分:0)
Sou May应该使用getRotation()或getOrientation()报告相同的值:
•0(默认,肖像),
•1(设备逆时针旋转90度),
•3(设备顺时针90度)
当你把它“放在头上”时它没有报告(旋转180,这将是值2)。该结果可能是特定于设备的。
首先,如果您使用模拟器或设备,则应明确说明。你知道如何旋转模拟器吗?然后我建议使用onCreate()方法创建一个小测试程序,如下所示:
*** -[PIBA.PreguntasViewController respondsToSelector:]: message sent to deallocated instance 0x7ae2dea0
检查设备的屏幕是否已在设备设置中锁定设置>显示>自动旋转屏幕。如果取消选中该复选框,Android将不会向您的活动报告方向更改。要明确:它不会重新启动活动。在我的情况下,我只得到0,就像你描述的那样。
如果将这些行添加到onCreate()
,则可以从程序中检查此项@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation());
}
Log.d(“ORIENTATION_TEST”,“从设备设置自动旋转屏幕:”+ sysAutoRotate);
如果自动旋转关闭则返回0,如果启用自动旋转则返回1。另一个尝试。在您的原始程序中,您可能已在清单中设置 机器人:screenOrientation = “纵向”