delphi xe6 调用java原生GPS的方法

2020-11-19 19:04

阅读:719

标签:des   android   style   class   div   java   c   size   t   ext   sp   

如果用xe6自带的LocationSensor控件,默认优先使用网络位置,为了直接使用GPS位置,在网上搜到了以下代码,经实测证实是可用的。
 
uses Androidapi.JNI.Location, Androidapi.JNIBridge, Androidapi.JNI.JavaTypes, 
  Androidapi.JNI.Os,FMX.Helpers.Android,Androidapi.JNI.GraphicsContentViewText;
 
type
  TLocationListener = class;
  TForm1 = class(TForm)
 
  . . . . . .
 
  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;
 
. . . . .
 
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;
 
destructor TForm1.Destroy;
begin
  if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
  inherited;
end;
 
procedure TForm1.onLocationChanged(location: JLocation);
begin
  if Assigned(location) then
  begin
     //variabili da recuperare dal sensore
     Label4.Text := location.getLatitude.ToString;
     Label5.Text := location.getLongitude.ToString;
     Label6.Text := location.getAltitude.ToString;
     Label8.Text := location.getSpeed.ToString;
     Label10.Text := location.getTime.ToString;
  end;
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.GPS_PROVIDER, 1000, 0, locationListener, TJLooper.JavaClass.getMainLooper);
  end;
 FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
  FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
  onLocationChanged(location);
end

delphi xe6 调用java原生GPS的方法,搜素材,soscw.com

delphi xe6 调用java原生GPS的方法

标签:des   android   style   class   div   java   c   size   t   ext   sp   

原文地址:http://www.cnblogs.com/qiufeng2014/p/3698926.html


评论


亲,登录后才可以留言!