我使用的是本地位置管理器,但是如果我关闭位置传感器然后再次打开,应用程序会在启动后立即崩溃(位置传感器图标只是闪烁)。但是,如果我嘲笑一个新位置,那么它不会崩溃并且工作正常。
Ps。:同时,当应用程序在启动时不断崩溃,如果我运行Waze,等待10秒,然后再次启动,它就可以运行。
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.JNI.Location, Androidapi.JNIBridge, Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os, FMX.Layouts, FMX.ListBox, FMX.StdCtrls,
FMX.Controls.Presentation, Androidapi.Helpers;
type
TLocationListener = class;
TForm1 = class(TForm)
Label1: TLabel;
procedure FormShow(Sender: TObject);
private
FLocationManager : JLocationManager;
locationListener : TLocationListener;
public
destructor Destroy; override;
procedure onLocationChanged(location: JLocation);
end;
TLocationListener = class(TJavaLocal, JLocationListener)
private
[weak]
FParent : TForm1;
public
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;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText;
{ TLocationListener }
constructor TLocationListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(location: JLocation);
begin
FParent.onLocationChanged(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;
{ TForm1 }
destructor TForm1.Destroy;
begin
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
inherited;
end;
procedure TForm1.FormShow(Sender: TObject);
var
LocationManagerService: JObject;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 2000, 20, locationListener,TJLooper.JavaClass.getMainLooper);
end;
location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
onLocationChanged(location);
end;
procedure TForm1.onLocationChanged(location: JLocation);
begin
Label1.Text := FloatToStr((location.getSpeed*3600)/1000) + ' km/h';
end;
end.
我错过了什么?事件的哪个部分可能导致此次崩溃?