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.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
FMX.Edit;
type
TLocationListener = class;
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FLocationManager: JLocationManager;
locationListener: TLocationListener;
public
destructor Destroy; override;
{ Public declarations }
procedure onLocationChanged(Location: JLocation);
end;
// Sensore GPS
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}
{ TLocationListener }
constructor TLocationListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(Location: JLocation);
begin
end;
procedure TLocationListener.onProviderDisabled(provider: JString);
begin
end;
procedure TLocationListener.onProviderEnabled(provider: JString);
begin
end;
destructor TForm1.Destroy;
begin
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
inherited;
end;
procedure TLocationListener.onStatusChanged(provider: JString; status: Integer;
extras: JBundle);
begin
end;
procedure TForm1.FormCreate(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.NETWORK_PROVIDER, 0, 0, locationListener,
// FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
FLocationManager.isProviderEnabled
(TJLocationManager.JavaClass.NETWORK_PROVIDER);
onLocationChanged(Location);
end;
procedure TForm1.onLocationChanged(Location: JLocation);
begin
if Assigned(Location) then
begin
// variabili da recuperare dal sensore
Edit1.Text := Location.getLatitude.ToString;
Edit2.Text := Location.getLongitude.ToString;
Edit3.Text := Location.getAltitude.ToString;
Edit4.Text := Location.getSpeed.ToString;
Edit5.Text := Location.getTime.ToString;
end;
end;
end.
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.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
FMX.Edit;
type
TLocationListener = class;
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FLocationManager: JLocationManager;
locationListener: TLocationListener;
public
destructor Destroy; override;
{ Public declarations }
procedure onLocationChanged(Location: JLocation);
end;
// Sensore GPS
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}
{ TLocationListener }
constructor TLocationListener.Create(AParent: TForm1);
begin
inherited Create;
FParent := AParent;
end;
procedure TLocationListener.onLocationChanged(Location: JLocation);
begin
end;
procedure TLocationListener.onProviderDisabled(provider: JString);
begin
end;
procedure TLocationListener.onProviderEnabled(provider: JString);
begin
end;
destructor TForm1.Destroy;
begin
if Assigned(locationListener) then
FLocationManager.removeUpdates(locationListener);
inherited;
end;
procedure TLocationListener.onStatusChanged(provider: JString; status: Integer;
extras: JBundle);
begin
end;
procedure TForm1.FormCreate(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.NETWORK_PROVIDER, 0, 0, locationListener,
TJLooper.JavaClass.getMainLooper);
//TJLocationManager.JavaClass.NETWORK_PROVIDER 这个参数可改
// FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
FLocationManager.isProviderEnabled
(TJLocationManager.JavaClass.NETWORK_PROVIDER);
onLocationChanged(Location);
end;
procedure TForm1.onLocationChanged(Location: JLocation);
begin
if Assigned(Location) then
begin
// variabili da recuperare dal sensore
Edit1.Text := Location.getLatitude.ToString;
Edit2.Text := Location.getLongitude.ToString;
Edit3.Text := Location.getAltitude.ToString;
Edit4.Text := Location.getSpeed.ToString;
Edit5.Text := Location.getTime.ToString;
end;
end;
end.