Delphi 10.3,Android API:28,手机:三星 Galaxy A52S 5G
我创建了一个空白的 FMX 应用程序。 LocationSensor只是形式。 (有效:=真) 添加用户权限:访问粗略位置和访问精细位置
手机 GPS 处于活动状态。
有什么问题吗?
这是代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation,
FMX.StdCtrls, System.Sensors, System.Sensors.Components;
type
TForm1 = class(TForm)
LocationSensor1: TLocationSensor;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
我无法帮助您使用 LocationSensor 组件,因为我只是在第一次 Android 开发的早期尝试过它。但是,您可以使用 API 实现 GPS 功能。在应用程序“稳定”并且 UI 变为活动状态之前,请勿调用
StartAndroidGps
。例如。用户登录后。
uses
, Androidapi.JNI.Provider
, Androidapi.JNI.JavaTypes
, Androidapi.Helpers
, Androidapi.JNI.Location
, Androidapi.JNIBridge
, FMX.Helpers.Android
, Androidapi.JNI.GraphicsContentViewText
, Androidapi.JNI.Net
, AndroidApi.JNI.Embarcadero
, Androidapi.JNI.Os
, System.Android.Sensors
, System.Android.Service
private
FLocationManager : JLocationManager;
locationListener : TLocationListener;
procedure LocationisChanged(location: JLocation);
procedure StartAndroidApi;
procedure StopAndroidApi;
...
procedure TfrmTabbed.StartAndroidGPS;
var
LocationManagerService: JObject;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
end;
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER,
15000, 30, locationListener,
TJLooper.JavaClass.getMainLooper); // see the Android doc's
// just to get started, ask for Last Known Location if any
LogMe('GPS started');
location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
LocationisChanged(location);
end;
procedure TfrmTabbed.StopAndroidGPS;
begin
LogMe('GPS stopped');
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
end;
{ TLocationListener }
constructor TLocationListener.Create(AParent: TfrmTabbed);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(location: JLocation);
begin
FParent.LocationisChanged(location);
end;
procedure TLocationListener.onProviderDisabled(provider: JString);
begin
end;
procedure TLocationListener.onProviderEnabled(provider: JString);
begin
end;
procedure TfrmTabbed.LocationisChanged(location: JLocation);
begin
if FisClosing then
EXIT; // the code causes and AV if the app is already closing
if Assigned(location) then
begin
// Calling the LocationSensor's method :-)
LocationSensor1LocationChanged(Self,
TLocationCoord2D.Create(0,0),
TLocationCoord2D.Create(location.getLatitude, location.getLongitude))
end;
end;
procedure TLocationListener.onStatusChanged(provider: JString; status: Integer;
extras: JBundle);
begin
end;