23

Hello I want to use the type type_orientation but it is deprecated for the android 4.0.3 . I use it for the augmented reality. I tried to find on the net but without success. My application works but my text inside stay in the same place! Here my code:

 @Override
    protected void onStart() {      
        super.onStart();
        sensorMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorLstr = createListener();
        sensorMngr.registerListener(sensorLstr, sensorMngr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onStop() {       
        super.onStop();
        sensorMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorMngr.unregisterListener(sensorLstr, sensorMngr.getDefaultSensor(Sensor.TYPE_ORIENTATION));        
    }

Everything it s good just this, i looked already on http://developer.android.com/reference/android/hardware/Sensor.html, but i can t understand (I m french and my english is not very perfect...) Thank you very much for your help!!!

ThinkingStiff
  • 62,391
  • 29
  • 139
  • 237
mhsc90
  • 234
  • 1
  • 2
  • 11

1 Answers1

18

Here's a link to a blog with a solution. Essentially, you stop using the Orientation Sensor and use the Magnetic Field Sensor and Accelerometer Sensor(s) in tandem to get equivalent functionality. It's more work but it does allow you to continue to use a callback to handle orientation changes.

EDIT: Since the original site is no longer available, here's the code example that was previously hosted there:

package com.samplecompass;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

public class CompassActivity extends Activity implements SensorEventListener {

  Float azimut;  // View to draw a compass

  public class CustomDrawableView extends View {
    Paint paint = new Paint();
    public CustomDrawableView(Context context) {
      super(context);
      paint.setColor(0xff00ff00);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
    };

    protected void onDraw(Canvas canvas) {
      int width = getWidth();
      int height = getHeight();
      int centerx = width/2;
      int centery = height/2;
      canvas.drawLine(centerx, 0, centerx, height, paint);
      canvas.drawLine(0, centery, width, centery, paint);
      // Rotate the canvas with the azimut      
      if (azimut != null)
        canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);
      paint.setColor(0xff0000ff);
      canvas.drawLine(centerx, -1000, centerx, +1000, paint);
      canvas.drawLine(-1000, centery, 1000, centery, paint);
      canvas.drawText("N", centerx+5, centery-10, paint);
      canvas.drawText("S", centerx-10, centery+15, paint);
      paint.setColor(0xff00ff00);
    }
  }

  CustomDrawableView mCustomDrawableView;
  private SensorManager mSensorManager;
  Sensor accelerometer;
  Sensor magnetometer;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCustomDrawableView = new CustomDrawableView(this);
    setContentView(mCustomDrawableView);    // Register the sensor listeners
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
      accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  }

  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
  }

  protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
  }

  public void onAccuracyChanged(Sensor sensor, int accuracy) {  }

  float[] mGravity;
  float[] mGeomagnetic;
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
      mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
      mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
      float R[] = new float[9];
      float I[] = new float[9];
      boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
      if (success) {
        float orientation[] = new float[3];
        SensorManager.getOrientation(R, orientation);
        azimut = orientation[0]; // orientation contains: azimut, pitch and roll
      }
    }
    mCustomDrawableView.invalidate();
  }
}
Nick
  • 7,855
  • 2
  • 36
  • 62
  • It works now, I forgot to convert the azimuth to degrees. Thanks! – Levi Aug 31 '13 at 16:49
  • 1
    i guess `ORIENTATION_SENSOR` is common in all devices! does MAGNETIC & ACCELEROMETER is also common? – Muhammad Babar Oct 28 '14 at 10:35
  • 1
    The magnetometer unfortunately is not a universal sensor. The Moto G3 (3rd gen) for example does not have one. – Nick Nov 20 '17 at 13:34
  • 6
    Webpage not available – vihkat Feb 05 '19 at 07:56
  • I was able to find a [cached version](http://web.archive.org/web/20151205103652/http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html) of the site and have added the source example from there to this post. – Nick Mar 25 '20 at 13:22
  • @Nick so what is the solution if any device don't have magnetometer? – Shrimantee Roy Jun 08 '20 at 10:48