delphi TLocationSensor激活时FMX应用程序崩溃

lxkprmvk  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(155)

Delphi 10.3,Android API:28,电话:三星Galaxy A52 S 5G
我创建了一个空白的FMX应用程序。定位传感器只是形式。(Active:= True)已添加用户权限:访问粗略位置和访问精细位置
手机GPS激活。

  • 如果运行应用程序,没有位置权限,OK,没有崩溃!位置传感器未激活
  • 如果手机添加了定位权限(Always),并且没有使用准确的定位,OK,不死机!位置传感器未激活
  • 如果手机添加位置权限(始终),并使用准确的位置,不好,崩溃,但LocationSensor是活跃的!

有什么问题吗?
这是代码:

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}
esbemjvw

esbemjvw1#

我不能帮助你定位传感器组件,因为我只在我第一次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;

  TLocationListener = class(TJavaLocal, JLocationListener)
  private
    [weak]
    FParent : TfrmTabbed;
  public
    constructor Create(AParent : TfrmTabbed);
    procedure onLocationChanged(location: JLocation); cdecl;
    procedure onProviderDisabled(provider: JString); cdecl;
    procedure onProviderEnabled(provider: JString); cdecl;
    procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
  end;

...

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 TLocationListener.onStatusChanged(provider: JString; status: Integer;
  extras: JBundle);
begin
end;

procedure TfrmTabbed.LocationisChanged(location: JLocation);
begin

  if FisClosing then
    EXIT; // the code causes an 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;

相关问题