delphi xe10 android service gps

时间:2015-12-07 11:39:31

标签: android delphi service gps

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

2 个答案:

答案 0 :(得分:1)

查看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;

答案 1 :(得分:1)

如果TTimer没有按预期工作,请尝试在匿名线程中执行所有操作并在主线程中使用TThread.CurrentThread.Sleep

function TServiceModule.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
  Result := TJService.JavaClass.START_STICKY;

  TThread.CreateAnonymousThread(procedure
    begin
      repeat
        TThread.Current.Sleep(MSecsPerSec);
        TThread.CreateAnonymousThread(procedure
          DoStuff();
        end).Start;
        TThread.Current.Sleep(MSecsPerSec);
      until (TThread.Current.CheckTerminated);
    end).Start;
end;