What is the alternative to android orientation sensor?
There is no actual "orientation" sensor - this is (was) actually a composite sensor, generated from a combination of the accelerometer and the magnometer.
From http://developer.android.com/reference/android/hardware/SensorEvent.html
"This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead."
public class OrientationTestActivity extends Activity implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
protected void onResume() {
super.onResume();
mLastAccelerometerSet = false;
mLastMagnetometerSet = false;
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
Log.i("OrientationTestActivity", String.format("Orientation: %f, %f, %f",
mOrientation[0], mOrientation[1], mOrientation[2]));
}
}
}
Sensor.TYPE_ROTATION_VECTOR has drift: Is there an alternative?
https://play.google.com/store/apps/details?id=org.hitlabnz.sensor_fusion_demo has some handy alternatives I just found. I still have to test which fusion leads to most steady results.
Source here: https://bitbucket.org/apacha/sensor-fusion-demo/src .
A lot of these also show drift; in particular the ones which use the gyro.
Locking ScreenOrientation
In general onConfigurationChanged()
event fire only when an configuration change has occured. In your app the orientation changed event occurs only when the screen is free to rotate. If you have locked the screen orientation then the screen orientation event is not fired. onConfigurationChanged()
does not listen to the sensor that is responsible to rotate the device but fires only when the appropriate event is happening.
So what you really want is to have access to a SensorManager
and attach a SensorListener
. This is the way for you to listen to the actual orientation of the device.
Here is a very nice demo that demonstrates the SensorManager
capabilities with the orientation of the phone:
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
UPDATE: The orientation sensor is a composite sensor to make things easier for the developer. It does not actually exist in the phone. Is a very neat sensor combining the accelometer sensor and the magnetic field sensor. And the OrientationSensor is currently deprecetated according to the docs (http://developer.android.com/guide/topics/sensors/sensors_position.html).
See What is the alternative to android orientation sensor? for sample usage.
it may need some fixing I have not tested it much.
Alternative of openintents for simulating sensors for Android
Calling SensorManager.connectSimulator()
constitutes a network operation as shown by your stack trace. Thus, you're attempting to perform network operations on your application's main thread. Android does not allow this by default and so you have two options.
- Use another thread or async task. The AsyncTask syntax and usage convention can be a bit confusing, so here's an example: AsyncTask Android example.
- Turn off strict mode (not recommended).
Related Topics
How to Test If a Fragment View Is Visible to the User
Update Text of Notification, Not Entire Notification
Video Streaming Using Rtsp: Android
Android Emulator Won't Run Application Started from Eclipse
Android Set Button Background Programmatically
Signal 11 Sigsegv Crash Android
How to Save the Image to Sd Card on Button Click Android
Detect When Android V2 Maps Has Loaded
Android Drawerlayout - No Drawer View Found with Gravity
Can't Upload Updated APK to Google Play Due to Different Certificate
How to Remove the Top and Bottom Space on Textview of Android
How to Use Dimens.Xml in Android
Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception
How to Do Page Flip/Turn/Curl Effect in Android
Find Out the Running Process Id by Package Name
Execute Task Before Android Gradle Build
Get Gps Location in a Broadcast Receiver/Or Service to Broadcast Receiver Data Transfer