xe6 调用JAVA标准功能

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,

      TJLooper.JavaClass.getMainLooper);

//TJLocationManager.JavaClass.NETWORK_PROVIDER 这个参数可改

  end;
  // 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值