問題:XE8 for Android 方向傳感器無法正常運作(在 XE7 是正常的)
測試:官方示例 Samples\Object Pascal\Mobile Snippets\OrientationSensor\
適用:XE8 for Android
修復方法:
請將源碼 System.Android.Sensors.pas 復制到自己的工程目錄裡,再進行修改。
修改下面代碼:
TAndroidNativeMagneticSensor = class(TCustomOrientationSensor) strict private FNativeSensor: TNativeSensor; protected function GetUpdateInterval: Double; override; procedure SetUpdateInterval(AInterval: Double); override; function GetOrientationSensorType: TOrientationSensorType; override; {+++>} // 加入下面二行代碼 function DoStart: Boolean; override; procedure DoStop; override; {<+++} function GetSensorCategory: TSensorCategory; override; function GetState: TSensorState; override; function GetTimeStamp: TDateTime; override; function GetAvailableProperties: TCustomOrientationSensor.TProperties; override; function GetDoubleProperty(Prop: TCustomOrientationSensor.TProperty): Double; override; public constructor Create(AManager: TSensorManager); override; function Supported: Boolean; end;
找到 TAndroidNativeMagneticSensor.Create 加入下面代碼:
... 略 ... constructor TAndroidNativeMagneticSensor.Create(AManager: TSensorManager); begin inherited; FNativeSensor := TNativeSensor.Create(ASENSOR_TYPE_MAGNETIC_FIELD); end; {+++>} // 加入下面代碼 function TAndroidNativeMagneticSensor.DoStart: Boolean; begin Result := FNativeSensor.DoStart; end; procedure TAndroidNativeMagneticSensor.DoStop; begin inherited; FNativeSensor.DoStop; end; {<+++} // 加入上面代碼 ... 略 ...