I'm using Emabarcadero Delphi XE 10
Seattle Update 1 to create an Android service, i've found this tutorial Link and created a service application, what i want to do is get current location in every 5 seconds with Location Sensor and save to database. I'm doing that on my mainform but when i close the program it's stopping, so i couldn't find any article about how i can syncronize my application and service. I've tried to add timer and Locationsensor to service form but Delphi is not allow me to add to service form any object,i think i need to send a command to service or somehow i need to connect service and program, i found a sample which shows how to download images with service but it's too expert for a rookie like me :)
Asked
Active
Viewed 2,956 times
4

Abhinav singh
- 1,448
- 1
- 14
- 31

BMF
- 111
- 1
- 12
-
5Related (for Java): http://stackoverflow.com/questions/8828639/android-get-gps-location-via-a-service – mjn Dec 07 '15 at 11:46
-
In addition to the service you will need the AlarmManager to call the service repeatedly – mjn Dec 07 '15 at 11:49
-
@mjn thanks for the heads up,i think i've missed that,thanks again. – BMF Dec 11 '15 at 06:49
-
Your application will require an expert to develop to completion. This is not a "rookie" task. – Freddie Bell Dec 14 '15 at 13:07
-
this question is not a duplicate of the question in the link. – nurettin May 26 '17 at 15:11
2 Answers
1
Look at this article. Daniele Spinetti solve problem using LocationSensor in service.
Put System.Sensors and System.Android.Sensors units to your project and make changes (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;
Then you can use LocationSensor like this:
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;

Rusland
- 159
- 2
- 13
-
-
-
It's working method. I use it in my app. However i stuck with timer - TTimer don't work in service. Any suggestion? – Rusland Feb 05 '16 at 07:17
1
if TTimer is not working as you expected try to do all your stuff in anonymous thread and use TThread.CurrentThread.Sleep in main thread
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;

Tomasz Andrzejewski
- 136
- 3