delphixe android服务,delphi xe10 android服务gps [复制]

参见英文答案 > Get GPS location via a service in Android                                    5个

我正在使用Emabarcadero Delphi XE 10 Seattle Update 1来创建一个Android服务,我发现这个教程Link并创建了一个服务应用程序,我想做的是使用Location Sensor每隔5秒获取一个当前位置并保存到数据库.我在我的主要表单上这样做但是当我关闭程序它停止时,所以我找不到任何关于如何同步我的应用程序和服务的文章.我试图将定时器和Locationsensor添加到服务表单但Delphi不允许我添加到服务表单任何对象,我想我需要发送命令服务或不知何故我需要连接服务和程序,我发现了一个示例这显示了如何使用服务下载图像,但对于像我这样的菜鸟来说它太专家了:)

解决方法:

看看this文章. Daniele Spinetti使用LocationSensor解决问题.

将System.Sensors和System.Android.Sensors单元放入您的项目并进行更改(Delphi 10 Seattle).

System.Sensors.pas

// about line 748

implementation

uses

System.Variants, System.Math, System.Character,

{$IFDEF ANDROID}

// -- patch

// include the modified System.Android.Sensors

System.Android.Sensors;

{$ENDIF ANDROID}

System.Android.Sensors.pas

//about line 12

uses

// -- patch

// use the modified System.Sensors

System.Sensors;

...

// about line 70

class constructor TPermission.Create;

var

PackageInfo: JPackageInfo;

PackageManager: JPackageManager;

Activity: JActivity;

LContext: JContext;

begin

// -- patch

// Activity := TJNativeActivity.Wrap

// (PANativeActivity(System.DelphiActivity)^.clazz)

LContext := TJContextWrapper.Wrap(System.JavaContext);

PackageManager := LContext.getPackageManager();

PackageInfo := PackageManager.getPackageInfo

(LContext.getApplicationContext.getPackageName,

TJPackageManager.JavaClass.GET_PERMISSIONS);

FPermissions := PackageInfo.requestedPermissions;

end;

...

// about line 100

type

TAndroidGeocoder = class(TGeocoder)

private type

TGeocoderRunnable = class(TJavaLocal, JRunnable)

private

FCoord: TLocationCoord2D;

FLGeocoder: JGeocoder;

public

constructor Create(ACoord: TLocationCoord2D; AGeocoder: JGeocoder);

procedure run; cdecl;

end;

private

class var

FGeocoder: JGeocoder;

// FActivity: JActivity; // -- patch

FActivity: JContextWrapper; // -- patch

...

// about line 130

TUIAndroidLocationSensor = class(TCustomLocationSensor)

private

FPermitted: Boolean;

// FActivity: JNativeActivity; // -- patch

FActivity: JContext; // -- patch

FLastValue: JLocation;

FLocationManager: JLocationManager;

FAccuracy: TLocationAccuracy;

...

// about line 1500

constructor TUIAndroidLocationSensor.Create(AManager: TSensorManager);

var

LocationService: JObject;

begin

inherited;

// FActivity := TJNativeActivity.Wrap

// (PANativeActivity(System.DelphiActivity)^.clazz); // -- patch

FActivity := TJContext.Wrap(System.JavaContext); // -- patch

LocationService := FActivity.getSystemService

(TJContext.JavaClass.LOCATION_SERVICE);

if Assigned(LocationService) then

FLocationManager := TJLocationManager.Wrap((LocationService as ILocalObject)

.GetObjectID);

end;

...

// about line 1530

function RunIfPossible(var ARunnable: TLocationRunnable;

var AListener: TLocationListener; AProviderName: JString): Boolean;

var

Provider: JLocationProvider;

LHandler: JHandler;

begin

Result := False;

if FLocationManager.isProviderEnabled(AProviderName) then

begin

if AListener = nil then

AListener := TLocationListener.Create(Self);

Provider := FLocationManager.getProvider(AProviderName);

if Provider <> nil then

begin

ARunnable := TLocationRunnable.Create(FLocationManager, AListener,

AProviderName);

// FActivity.runOnUiThread(ARunnable); // --patch

// -- patch

// You can use post method of Handler instead runOnUiThread in this case.

// more info here: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

LHandler := TJHandler.JavaClass.init;

LHandler.post(ARunnable);

Result := True;

end;

end;

end;

...

// about line 1730

class constructor TAndroidGeocoder.Create;

begin

// -- patch

// FActivity := TJNativeActivity.Wrap

// (PANativeActivity(System.DelphiActivity)^.clazz);

FActivity := TJContextWrapper.Wrap(System.JavaContext);

FGeocoder := TJGeocoder.JavaClass.init(FActivity);

end;

然后你可以像这样使用LocationSensor:

uses System.Sensors, System.Android.Sensors;

...

var

FSensors: TSensorArray;

Sensor: TCustomSensor;

begin

TSensorManager.Current.Active := true;

FSensors := TSensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);

FSensor := nil;

for Sensor in FSensors do

begin

if TCustomLocationSensor(Sensor).SensorType = TLocationSensorType.GPS then

begin

FSensor := TCustomLocationSensor(Sensor);

Break;

end;

end;

if not Assigned(FSensor) then

Exit; { no location sensor is available }

{ start the sensor if it is not started }

if not FSensor.Started then

FSensor.Start;

标签:android,gps,service,delphi

来源: https://codeday.me/bug/20190702/1356762.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值