Java Question Anonymous Subclass

MikieK

Member
Licensed User
Longtime User
I decompiled the Phone library to have a look at the PhoneSensors class (Sorry if this offends.)

B4X:
public boolean StartListening(BA ba, String EventName)
{
SensorManager sm = (SensorManager)BA.applicationContext.getSystemService("sensor");
String s = String.valueOf(EventName.toLowerCase(BA.cul)) + "_sensorchanged";
[B][U]this.listener = new Phone.PhoneSensors.1(this, ba, s);[/U][/B]

return sm.registerListener(this.listener, sm.getDefaultSensor(this.currentType), this.sensorDelay);
}

Im don't think that JD GUI has got the line in bold correct, and I get a type mismatch error when I change "Phone.PhoneSensors.1" to my main class name
(obviously because my main class doesn't have any variables as inputs)

but having said that, nothing in the Phone.PhoneSensors class requires 3 inputs. Any one know what the "this.listener = new Phone.PhoneSensors.1(this, ba, s);" should be?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is the code:
B4X:
/**
    * The PhoneSensors object allows you to listen for changes in one of the device sensors.
    *See the <link>Sensors example|http://www.b4x.com/forum/basic4android-getting-started-tutorials/6647-orientation-accelerometer.html</link>.
    *Most devices do not support all sensors. The StartListening method returns False if the sensor is not supported.
    *After initializing the object and calling StartListening, the SensorChanged event will be raised each time the sensor value changes.
    *The value is passed as an array of Floats. Some sensors pass a single value and some pass three values. 
    */
   @ShortName("PhoneSensors")
   @Events(values={"SensorChanged (Values() As Float)"})
   public static class PhoneSensors{
      /**
       * Single value - Ambient light level measured in SI lux units.
       */
      public static int TYPE_LIGHT = Sensor.TYPE_LIGHT;
      /**
       * Three values - Ambient magnetic field measured in micro-Tesla for the X, Y and Z axis.
       */
      public static int TYPE_MAGNETIC_FIELD = Sensor.TYPE_MAGNETIC_FIELD;
      /**
       * Single value - Atmospheric pressure.
       */
      public static int TYPE_PRESSURE = Sensor.TYPE_PRESSURE;
      /**
       * Single value - Proximity measured in centimeters. Most devices will return only two possible values representing "near" and "far".
       *"far" should match MaxRange and "near" should be a value smaller than MaxRange.
       */
      public static int TYPE_PROXIMITY = Sensor.TYPE_PROXIMITY;
      /**
       * Single value - Ambient temperature.
       */
      public static int TYPE_TEMPERATURE = Sensor.TYPE_TEMPERATURE;
      /**
       * Three values - Angular velocity measured in Radians / Second around each of the three axis.
       */
      public static int TYPE_GYROSCOPE = Sensor.TYPE_GYROSCOPE;
      /**
       * Three values - Acceleration measured in Meters / Second ^ 2 for each axis (X, Y and Z). 
       */
      public static int TYPE_ACCELEROMETER = Sensor.TYPE_ACCELEROMETER;
      /**
       * Three values - Orientation measured in degrees for azimuth, pitch and roll.
       */
      public static int TYPE_ORIENTATION = Sensor.TYPE_ORIENTATION;
      private int currentType;
      private int sensorDelay;
      private SensorEventListener listener;
      /**
       * Initializes the object and sets the sensor type (one of the TYPE_ constants).
       */
      public void Initialize(int SensorType) {
         Initialize2(SensorType, 3);
      }
      /**
       * Initializes the object and sets the sensor type and sensor events rate.
       *SensorType - One of the TYPE_ constants.
       *SensorDelay - A value between 0 (fastest rate) to 3 (slowest rate). This is only a hint to the system.
       */
      public void Initialize2(int SensorType, int SensorDelay) {
         this.currentType = SensorType;
         this.sensorDelay = SensorDelay;
      }
      /**
       * Starts listening for sensor events.
       *Returns True if the sensor is supported.
       *Usually you will want to start listening in Sub Activity_Resume and stop listening in Sub Activity_Pause.
       */
      public boolean StartListening(final BA ba, String EventName) {
         SensorManager sm = (SensorManager) BA.applicationContext.getSystemService(Context.SENSOR_SERVICE);
         final String s = EventName.toLowerCase(BA.cul) + "_sensorchanged";
         listener = new SensorEventListener() {

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

            @Override
            public void onSensorChanged(SensorEvent event) {
               ba.raiseEvent(PhoneSensors.this, s, event.values);
            }

         };
         return sm.registerListener(listener, sm.getDefaultSensor(currentType), sensorDelay);
      }
      /**
       * Stops listening for events.
       */
      public void StopListening(BA ba) {
         if (listener != null) {
            SensorManager sm = (SensorManager) ba.context.getSystemService(Context.SENSOR_SERVICE);
            sm.unregisterListener(listener);
         }
      }
      /**
       * Returns the maximum value for this sensor.
       *Returns -1 if this sensor is not supported.
       */
      public float getMaxValue() {
         java.util.List<Sensor> l = ((SensorManager) BA.applicationContext.getSystemService(Context.SENSOR_SERVICE)).getSensorList(currentType);
         if (l == null || l.size() == 0)
            return -1;
         return l.get(0).getMaximumRange();
      }


   }
 
Top