Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 联想A3000如何实现设备轮换?_Android_Rotation_Orientation - Fatal编程技术网

Android 联想A3000如何实现设备轮换?

Android 联想A3000如何实现设备轮换?,android,rotation,orientation,Android,Rotation,Orientation,萨拉姆。我从三星设备上安卓4.2的陀螺仪上获取数据,但在联想a3000上一无所获, 在三星,我的文本视图前面有旋转,但在联想,我什么都没有,甚至没有。它是空的。 我应该如何阅读联想A3000中的旋转(横滚、俯仰、航向)? 我的代码如下: public class CalculateDataService extends Service{ final static String TAG = "Roshan java"; SensorManager sensorManager; priv

萨拉姆。我从三星设备上安卓4.2的陀螺仪上获取数据,但在联想a3000上一无所获, 在三星,我的文本视图前面有旋转,但在联想,我什么都没有,甚至没有。它是空的。 我应该如何阅读联想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;
}

}

Sou可能应该使用getRotation()或getOrientation()报告相同的值:

•0(默认值,纵向)

•1(装置逆时针90度)

•3(装置顺时针90度)

当您将其“置于其头部”(旋转180,即值2)时,它不会报告。此结果可能是特定于设备的

首先,你应该弄清楚你是使用模拟器还是设备。你知道如何旋转模拟器吗?然后,我建议使用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());
}
在设备设置>显示>自动旋转屏幕中检查设备的屏幕是否已锁定。如果未选中该复选框,Android将不会报告您活动的方向更改。需要明确的是:它不会重新启动活动。在我的情况下,我只得到0,就像你描述的那样

如果将这些行添加到onCreate()中,则可以从程序中检查这一点

Log.d(“方向测试”,“设备设置的自动旋转屏幕:”+sysAutoRotate)

如果“自动旋转”处于禁用状态,则返回0;如果“自动旋转”处于启用状态,则返回1。再来一次。在原始程序中,您可能已经在清单中设置了
android:screenOrientation=“Picture”

谢谢你,伙计,但我需要设备的滚动、俯仰和航向角度。比如,为什么使用类型_方向而不是类型_陀螺仪?->我的设备没有陀螺仪传感器。但是你确定它能感知方向吗?
int sysAutoRotate = 0;
try {
    sysAutoRotate = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
} catch (SettingNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}