You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi实现Androidapi::Jni::Location监听时崩溃问题排查

问题:Delphi实现Android JNI位置监听器触发onLocationChanged时崩溃

之前尝试用CBuilder实现Androidapi::Jni::Location遇到瓶颈,转而按建议在Delphi中实现JNI位置监听器,再由C主窗体调用。编写了两个测试示例:一个是C++Builder调用Pascal函数创建监听器,另一个是纯Delphi版本。两款示例均能正常启动,但当监听器触发onLocationChanged时,应用无报错直接崩溃,已确认开启精确定位权限。

以下为纯Delphi版本代码:

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.Memo.Types,
  FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls,
  Androidapi.JNI.Provider ,Androidapi.Helpers, Androidapi.JNI.App,
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Os,
  Androidapi.JNI.Location, Androidapi.JNIBridge, Androidapi.JNI.JavaTypes;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FLocationManager: JLocationManager;
    FLocationManagerService: JObject;
    locationListener: JLocationListener;
  end;

  TLocationListener = class(TJavaLocal, JLocationListener)
private
  [weak] FParent : TForm1;

  constructor Create(AParent : TForm1);
  procedure onLocationChanged(location: JLocation); cdecl;
  procedure onProviderDisabled(provider: JString); cdecl;
  procedure onProviderEnabled(provider: JString); cdecl;
  procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
public
end;


var
  Form1: TForm1;
  flg_JNI_new_loc  : Boolean;
  JNI_lat, JNI_lon : Double;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
  LocationManagerService: JObject;
  location: JLocation;
begin
  Memo1.Lines.Append('Starting JNI GPS listener service...');

  if not Assigned(FLocationManager) then
  begin
    Memo1.Lines.Append('FLocation Manager not assigned');
    LocationManagerService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
    if not Assigned(locationListener) then
     begin
     Memo1.Lines.Append('locationListener not assigned');
     locationListener := TLocationListener.Create(self);
     end
    else
     Memo1.Lines.Append('locationListener already assigned');
   end
   else
    Memo1.Lines.Append('FLocation Manager already assigned');

   FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 1000, 30, locationListener,TJLooper.JavaClass.getMainLooper);
   Memo1.Lines.Append('Done');

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
if flg_JNI_new_loc = True then
begin
  flg_JNI_new_loc  := false;
  Memo1.Lines.Append('New loc '+Format('%.2f',  [JNI_lat])+' '+Format('%.2f', [JNI_lon]));
  end  ;
end;

//..............................................................................
{ TLocationListener }
constructor TLocationListener.Create(AParent: TForm1);
begin
  inherited Create;
  flg_JNI_new_loc := false;
  FParent := AParent;
end;


procedure TLocationListener.onLocationChanged(location: JLocation);
begin

  JNI_lat:=location.getLatitude;
  JNI_lon:=location.getLongitude;
  flg_JNI_new_loc:=true;
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;

end.

点击Button1创建监听器,户外环境下约1秒后触发onLocationChanged时应用崩溃。使用Embarcadero 11.1.5试用版,手机运行Android Pie,请问代码存在什么问题?


内容的提问来源于stack exchange,提问作者Tim Beurk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:45:28