Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi 10 Seattle for Android Service Notification for Location


This question is answered.


Permlink Replies: 9 - Last Post: Mar 28, 2017 12:44 PM Last Post By: Dave Nottage
eldi munggaran

Posts: 6
Registered: 10/5/06
Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2015 9:46 PM
I would like to create application that remains in background and sending location everytime the device moving / change location.

So I successfully tried 2 different apps:
1. Simple Mobile Android Apps with TLocation component, it can show location (I found in Demo folder)
2. Simple Mobile Android Apps + service for notification (I found in Demo folder as well).

then I try to make the notification to send my location by putting TLocation component inside the Service data module, but then when I run the apps it instantly crash by the time it starts the Service. Could someone help me why this happen?, what should I do?
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Delphi 10 Seattle for Android Service Notification for Location
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 12, 2015 11:08 AM   in response to: eldi munggaran in response to: eldi munggaran
Am 12.11.2015 um 06:46 schrieb eldi munggaran:
I would like to create application that remains in background and sending location everytime the device moving / change location.

So I successfully tried 2 different apps:
1. Simple Mobile Android Apps with TLocation component, it can show location (I found in Demo folder)
2. Simple Mobile Android Apps + service for notification (I found in Demo folder as well).

then I try to make the notification to send my location by putting TLocation component inside the Service data module, but then when I run the apps it instantly crash by the time it starts the Service. Could someone help me why this happen?, what should I do?

Hello,

afaik you cannot use FMX stuff in Android services. You have to look
inside TLocation in order to find out how the ANdroid specific
implementation is done using the Android Java style/based API via the
Java bridge Delphi uses.

You have to do it similiar to them in your service but have to make sure
you do not use any FMX stuff in your version.

Greetings

Markus
eldi munggaran

Posts: 6
Registered: 10/5/06
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 12, 2015 3:04 PM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:
Am 12.11.2015 um 06:46 schrieb eldi munggaran:
I would like to create application that remains in background and sending location everytime the device moving / change location.

So I successfully tried 2 different apps:
1. Simple Mobile Android Apps with TLocation component, it can show location (I found in Demo folder)
2. Simple Mobile Android Apps + service for notification (I found in Demo folder as well).

then I try to make the notification to send my location by putting TLocation component inside the Service data module, but then when I run the apps it instantly crash by the time it starts the Service. Could someone help me why this happen?, what should I do?

Hello,

afaik you cannot use FMX stuff in Android services. You have to look
inside TLocation in order to find out how the ANdroid specific
implementation is done using the Android Java style/based API via the
Java bridge Delphi uses.

You have to do it similiar to them in your service but have to make sure
you do not use any FMX stuff in your version.

Greetings

Markus

Thank you for clear explaination, I was thought Delphi filtered this feature which will not show any component if not going to be compatible (like puting firemonkey component into VCL), so I was wrong.
So I will try to extract TLocation with Android specific implementation and this will be the first time for me, do you have any tutorial/example?
Francisco Peris

Posts: 91
Registered: 1/5/15
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2015 10:34 AM   in response to: eldi munggaran in response to: eldi munggaran
Hi:

I have just found this thread and I have discovered that we cannot us fmx stuff in a service, even we can drop them on the "false" form.

I need to do the same, GPS location on a service.

Can anybody help us?

Thanks in advance
Francisco Peris

Posts: 91
Registered: 1/5/15
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2015 11:17 AM   in response to: Francisco Peris in response to: Francisco Peris
Francisco Peris wrote:
Hi:

I have just found this thread and I have discovered that we cannot us fmx stuff in a service, even we can drop them on the "false" form.

I need to do the same, GPS location on a service.

Can anybody help us?

Thanks in advance

In this link they say we can access the gps in a different way.

http://www.fmxexpress.com/directly-access-the-gps-location-sensor-with-the-jni-in-delphi-xe7-firemonkey-on-android/

Is this a good approach for a service?

Francisco Peris

Posts: 91
Registered: 1/5/15
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2015 1:42 PM   in response to: eldi munggaran in response to: eldi munggaran
I am still playing.

The code from the previous link works Seattle after some small changes. I copy it here:

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.Layouts, FMX.ListBox, FMX.StdCtrls, Androidapi.Helpers,
  FMX.Controls.Presentation;
 
type
 
  TLocationListener = class;
 
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
 
  private
    FLocationManager : JLocationManager;
    locationListener : TLocationListener;
  public
    destructor Destroy; override;
    { Public declarations }
    procedure onLocationChanged(location: JLocation);
  end;
 
  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}
 
uses FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText;
 
{ TLocationListener }
 
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;
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
var
  LocationManagerService: JObject;
  iter : JIterator;
  location : JLocation;
begin
  if not Assigned(FLocationManager) then
  begin
    //LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    LocationManagerService := TAndroidHelper.Context.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, 10000, 10, locationListener,
    //    TJLooper.JavaClass.getMainLooper);
    FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 1000, 0, locationListener,
        TJLooper.JavaClass.getMainLooper);
  end;
  iter := FLocationManager.GetAllProviders.Iterator;
  ListBox1.Clear;
  while iter.hasNext do
  begin
    ListBox1.Items.Add(JStringToString(iter.next.ToString));
  end;
  CheckBox1.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
  CheckBox2.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
  location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  onLocationChanged(location);
end;
 
destructor TForm1.Destroy;
begin
  if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
  inherited;
end;
 
procedure TForm1.onLocationChanged(location: JLocation);
begin
  Label4.Text := location.getLatitude.ToString;
  Label5.Text := location.getLongitude.ToString;
  Label6.Text := location.getAltitude.ToString;
 
end;
 
end.

However, it does not refresh the location. And I see that some people is complaining about the same in other forums. No way to refresh the location. it seems that only shows the last location found.

Any help?

Bahadır ATILGAN

Posts: 2
Registered: 5/12/13
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 28, 2017 7:32 AM   in response to: Francisco Peris in response to: Francisco Peris
Francisco Peris wrote:
I am still playing.

The code from the previous link works Seattle after some small changes. I copy it here:

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.Layouts, FMX.ListBox, FMX.StdCtrls, Androidapi.Helpers,
  FMX.Controls.Presentation;
 
type
 
  TLocationListener = class;
 
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
 
  private
    FLocationManager : JLocationManager;
    locationListener : TLocationListener;
  public
    destructor Destroy; override;
    { Public declarations }
    procedure onLocationChanged(location: JLocation);
  end;
 
  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}
 
uses FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText;
 
{ TLocationListener }
 
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;
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
var
  LocationManagerService: JObject;
  iter : JIterator;
  location : JLocation;
begin
  if not Assigned(FLocationManager) then
  begin
    //LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    LocationManagerService := TAndroidHelper.Context.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, 10000, 10, locationListener,
    //    TJLooper.JavaClass.getMainLooper);
    FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 1000, 0, locationListener,
        TJLooper.JavaClass.getMainLooper);
  end;
  iter := FLocationManager.GetAllProviders.Iterator;
  ListBox1.Clear;
  while iter.hasNext do
  begin
    ListBox1.Items.Add(JStringToString(iter.next.ToString));
  end;
  CheckBox1.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
  CheckBox2.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
  location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  onLocationChanged(location);
end;
 
destructor TForm1.Destroy;
begin
  if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
  inherited;
end;
 
procedure TForm1.onLocationChanged(location: JLocation);
begin
  Label4.Text := location.getLatitude.ToString;
  Label5.Text := location.getLongitude.ToString;
  Label6.Text := location.getAltitude.ToString;
 
end;
 
end.

However, it does not refresh the location. And I see that some people is complaining about the same in other forums. No way to refresh the location. it seems that only shows the last location found.

Any help?


TLocationListener = class (TJavaLocal, JLocationListener)
I did not write this code line for c ++ builder, I am constantly getting an error.
Can you help me?

Thanks
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 28, 2017 12:44 PM   in response to: Bahadır ATILGAN in response to: Bahadır ATILGAN
Bahadır ATILGAN wrote:

TLocationListener = class (TJavaLocal, JLocationListener)
I did not write this code line for c ++ builder, I am constantly getting an error.
Can you help me?

It would help to know what the error is.

--
Dave Nottage [MVP, TeamB]
Hints, tips and tricks at: http://www.delphiworlds.com/blog
Keith Marbach

Posts: 69
Registered: 8/21/01
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2016 12:04 PM   in response to: eldi munggaran in response to: eldi munggaran
I see the Delphi Seattle compatible code and I say thank you for that.

For my purposes I would like to geo-stamp certain events in my app, so I would like to call (an as of yet non-existant) GetGPS on demand (maybe sometimes every minute, sometimes not for hours), and I want the response to be fast, and I also want battery usage to be low.

Is that more than I should hope for right now?

My level of skill is decent with Delphi but starting intents and getting back results is pretty new to me. I understand the concept but seems like there are other pitfalls to that (like concert for BecameAcvtive firing when the intent finishes).

Thanks for any advice... this thread is close to what I'm hoping to find but not quite my requirements, which are:

1) ability to call for GPS coordinates on demand (no need for location sensor to continually run and keep track of location)
2) quick response when my app calls this currently non-existing GetGPS procedure,
3) low battery usage for the device
4) and, an understanding of device location settings that should either be enabled or not to get the most accurate locations we can

I'll admit, with 4 I'm just being a bit lazy, but I've searched dozens of times on-line to satisfy 1-3 and I always come up without a solution. Thanks for any help you can lend.
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Delphi 10 Seattle for Android Service Notification for Location  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 10, 2016 9:11 AM   in response to: Keith Marbach in response to: Keith Marbach
Am 09.03.2016 um 21:04 schrieb Keith Marbach:
I see the Delphi Seattle compatible code and I say thank you for that.

For my purposes I would like to geo-stamp certain events in my app, so I would like to call (an as of yet non-existant) GetGPS on demand (maybe sometimes every minute, sometimes not for hours), and I want the response to be fast, and I also want battery usage to be low.

Is that more than I should hope for right now?

My level of skill is decent with Delphi but starting intents and getting back results is pretty new to me. I understand the concept but seems like there are other pitfalls to that (like concert for BecameAcvtive firing when the intent finishes).

Thanks for any advice... this thread is close to what I'm hoping to find but not quite my requirements, which are:

1) ability to call for GPS coordinates on demand (no need for location sensor to continually run and keep track of location)
2) quick response when my app calls this currently non-existing GetGPS procedure,

I think this contradicts with 1, as using GPS means you have to know the
position of some sattelites and if that info is not current on your
device it will take a bit to prepare it. Unless you can use assisted GPS
where the current sattelite positions are afaik available via some web
AP. But this only works, if you have internet connectivity at the place
you are.

Greetings

Markus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02