-3

It is a possibly dumb question, probability my Java is just rudimentary :-( but I do not understand why this works (it is actually Android code, but I think this as a general Java question).

I do not understand how is it possible that the object mySensorEventListener is actually created???

  • I do not understand what does this code below actually do.
  • How does this code get called???
  • When is it called???

THIS IS THE PART I DO NOT UNDERSTAND

public SensorListener mySensorEventListener = new SensorListener() {

    @Override
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this) {
        }
    }
    @Override
    public void onAccuracyChanged(int sensor, int accuracy) {
    }
};


And this is the android Activity it belongs to:

public class RouteMapActivity extends Activity implements IRegisterReceiver {
private SensorManager mSensorManager;
private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mapView = new MapView(this, provider.getTileSource()
            .getTileSizePixels(), resProxy, provider);

    mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mySensorEventListener,
            SensorManager.SENSOR_ORIENTATION,
            SensorManager.SENSOR_DELAY_FASTEST);

    setContentView(mapView);
}

public SensorListener mySensorEventListener = new SensorListener() {

    @Override
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this) {
            float mHeading = values[0];
            mapView.setMapOrientation(-mHeading);
        }
    }

    @Override
    public void onAccuracyChanged(int sensor, int accuracy) {
    }
};
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

2 Answers2

7

There is no new

As pointed out in comments, there is a new in the first line, this kind of instantiation is called Anonymous Class.

I do not understand what does this code below actually do.

The synchronized block makes sure that notification of change in the sensors are not handled simultaneously, but one after the other.

How does this code get called??? When is it called???

This code gets called by the SensorManager upon changes in the orientation. This is what it was registered for:

mSensorManager.registerListener(mySensorEventListener,
        SensorManager.SENSOR_ORIENTATION,
        SensorManager.SENSOR_DELAY_FASTEST);
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Nice answer, but IMHO you should also point out that `synchronized(this)` is usually [not a very good idea](http://stackoverflow.com/questions/442564/avoid-synchronizedthis-in-java). – m0skit0 Sep 30 '14 at 17:03
  • Thanks MB, I am not sure I understand when `public SensorListener mySensorEventListener = new SensorListener() {...etc...` gets called, I see the `new` now :-) still do not get when the object is created – Lisa Anne Sep 30 '14 at 17:09
  • the `new` operator means object creation. Since this code is not inside a method but inside the class, and since this object is not static, it is created when the enclosing object (your `RouteMapActivity`) is created. – MByD Sep 30 '14 at 17:38
1

Maybe the reason you're confused is that you don't realize that the stuff between the { ... } braces is the creating of a class without the formal ...

public class SensorListener {

@Override
public void onSensorChanged(int sensor, float[] values) {
     synchronized (this) {
     }
}

@Override
public void onAccuracyChanged(int sensor, int accuracy) {
}

} 

then in your code you'd see ...

public SensorListener mySensorEventListener = new SensorListener();

Perhaps you don't realize that what you have above is the same as what I've written here. Does that help?

KSK
  • 149
  • 3
  • 15
  • Thanks KSK :-) but still don't get it :-( Now I understand that the stuff between the braces is a class definition, and I see the `new` but when/how does it get instantiated? Thanks KSK – Lisa Anne Sep 30 '14 at 17:13
  • The `new SensorListener()` is called before the constructor of the `RouteMapActivity` class would be called. It may be helpful to you (if you can) to step through your code with a good debugger. You'll see this line be executed just before the `RouteMapActivity` contructor (if there is one) is called. In other words, when the `RouteMapActivity` is instantiated, then the `SensorListener` is instantiated immediately thereafter. – KSK Oct 01 '14 at 20:53