Android Question Background location tracking

Sergey_New

Well-Known Member
Licensed User
Longtime User
Please tell me how to display data about the current location in example Background location tracking. They are shown in the example screenshot. I'm using SDK 29.
 
Solution
I forgot to stop GPS after position detection.
If needed
B4X:
Private Sub GPS_LocationChanged (Location1 As Location)
    Label2.Text = "LAT: " & Location1.Latitude
    Label3.Text = "LON: " & Location1.Longitude
    Button1.Enabled = True
    GPS.Stop  'This was missing
End Sub

DonManfred

Expert
Licensed User
Longtime User
Please tell me how to display data about the current location in example Background location tracking.
It is already part of the Example. A notification is generated.
Look at the example.

You don´t have another way than a notification. Your app is in the background and no visible activity is shown.

You can show the location however you like if your app is in the foreground.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
It is already part of the Example.
Thanks for the comment!
Added Log(Body):
Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Tracking location", Body, Main)
    Log(Body)
    Return notification
End Sub
About every 10 seconds I get the necessary parameters:
** Service (myday) Start **
Service started in foreground mode.
**Receiver(everyday)OnReceive**
Cannot start from a receiver in debug mode.
55.80177 / 38.46801
55.80163 / 38.46793
55.80152 / 38.46804
55.8016 / 38.46789
55.80186 / 38.46756

I would like that after the first result received, notifications would be deleted, or that the notification icon would disappear from the top line of the screen.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
long running background service
I don't need a long running service. I only need to get the location data once when it is found.
I don't speak English, there may be difficulties with machine translation of the text.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I should check the example, but after receiving the first position you can save it somewhere and then stop the service.
It's not clear if you need to receive the position when your app is opened or on background.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I need location data only when the application is open.
In this case I think that you can eliminate the notification part of code, when you get the position stop the service and use the received data as you need.
But I am not checking the code right now.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User

Sagenut,​

Please help if you have the opportunity to check the code.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try this.
Watch out because the example has the same name as the original one from Erel.
Keep them separated to avoid overwriting.
I tried to minimize it and stripped out the Service part.
 

Attachments

  • MyLocation.zip
    9.3 KB · Views: 120
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I forgot to stop GPS after position detection.
If needed
B4X:
Private Sub GPS_LocationChanged (Location1 As Location)
    Label2.Text = "LAT: " & Location1.Latitude
    Label3.Text = "LON: " & Location1.Longitude
    Button1.Enabled = True
    GPS.Stop  'This was missing
End Sub
 
Upvote 0
Solution

Sergey_New

Well-Known Member
Licensed User
Longtime User
Thanks, the example works! Is there a way to remove the notification icon?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
In my example there are no notifications.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I'm sorry, it's my mistake - I was running the previous version :(
Everything works perfectly.
I still have a question - if, when opening the application, I need to immediately go to another activity, will the location continue to be determined?
I will experiment...
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I still have a question - if, when opening the application, I need to immediately go to another activity, will the location continue to be determined?
No.
You need to switch to B4XPages for this, or you should maintain the GPS work on a Service as it was before.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I really suggest you to switch to B4XPages.
They make many things much easier.
Of course they are not the solution to everything and they have some limits that needs workaround, but most of the time you should go for them.
Anyway you should explain better, and nearly completely for how possible, what you are trying to achieve.
I think that I solved the initial question that started the thread, but then you are in need of some changes not told before (more than 1 activity...).
This make what I did useless and need other modifications.
To avoid this it's needed to know the whole idea.
If not it will be a loss of time for you and for us.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
switch to B4XPages
I'm not planning on switching to B4XPages yet, because, the project is big.
Yes, you solved the original question that started the thread (I made a note about this).
Once again thank you very much for your participation!
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Just to be clear, what I wrote it's just to help you and all the others to make requests in the better way possible.
It wasn't intended as a criticism to you. ;)
 
Upvote 0
Top