How to Track Your Users Location in the Background in Android - Android Studio Tutorial

  Рет қаралды 73,385

Philipp Lackner

Philipp Lackner

Жыл бұрын

Tracking the user's location in Android is often needed when your app is minimized. Thanks to foreground services that works really well and without many restrictions. In this video I'll show you how.
⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:
pl-coding.com/premium-courses...
💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
pl-coding.com/drop-table-ment...
Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
pl-coding.com/newsletter
Join this channel to get access to perks:
/ @philipplackner
Get the source code for this video here:
github.com/philipplackner/Bac...
Join my Discord server:
/ discord
Regular programming advice on my Instagram page: / _philipplackner_
Checkout my GitHub: github.com/philipplackner
You like my free content? Here you can buy me a coffee:
www.buymeacoffee.com/philippl...

Пікірлер: 173
@lifebylazy
@lifebylazy Жыл бұрын
"To provide a streamlined experience for short-running foreground services, devices that run Android 12 or higher can delay the display of foreground service notifications by 10 seconds, with a few exceptions. This change gives short-lived tasks a chance to complete before their notifications appear." - Android Docs, if anyone was curious about that weird delay behavior
@sahilkanabar8605
@sahilkanabar8605 Жыл бұрын
How can I be so lucky I just had a project with background tracking and here it is by best teacher like him on youtube .❤
@yeinsonjosesernahuamani9542
@yeinsonjosesernahuamani9542 Жыл бұрын
friend how would it be sent to a node.js server?
@sahilkanabar8605
@sahilkanabar8605 Жыл бұрын
@@yeinsonjosesernahuamani9542 sorry i could not understand you.
@yeinsonjosesernahuamani9542
@yeinsonjosesernahuamani9542 Жыл бұрын
@@sahilkanabar8605 How do I send the location to a server in node.js?
@user-eh2el2mt2n
@user-eh2el2mt2n Жыл бұрын
@@yeinsonjosesernahuamani9542 use retrofit?
@BapeRuLLZ
@BapeRuLLZ Жыл бұрын
I think we may be able to improve upon the DefaultLocationClient class by putting all the locationmanager checking methods and also the context extension function into usecase classes that are wrapped by interfaces and the DefaultLocationClient class would depend upon those. That way you could unit test the class better by providing different mock implementations for the interfaces so if you would want to write a test case that checks the output if for example the gps is enabled but the location is not, then that way you could easily do that. It might be overkill but I think every unit test in itself could seem like an overkill. Just my two cents.
@muhammadirfanrajput8400
@muhammadirfanrajput8400 Жыл бұрын
Android gives continuous pain by continuous changing android SDK
@ChrisAthanas
@ChrisAthanas Жыл бұрын
Its a constant clusterfuck for sure
@SEMPERFIDELIS7
@SEMPERFIDELIS7 Жыл бұрын
It sure is one hell of a shame!
@juansuarez5216
@juansuarez5216 Жыл бұрын
Congrats!! Really good tutorial, well explained and keeping it as simple as possible
@halvtysk
@halvtysk Жыл бұрын
Nice video, we have this setup in our app, albeit implemented before the introduction of flows. If you "take requests" for future videos, I would love to see your approach to handling automatic startup of these types of services when a BLE device is present. Our devices requires physical activation and for our app to be ready for this, we have to keep a sticky notification at all times which is ugly. There are workarounds to use "companion devices", but the support for proximity to BLE devices seems limited to the latter two (?) Android versions. It would be interesting to see how you would handle the "do stuff when in proximity"-approach for both newer Android devices as well as for older.
@chabbishreels1983
@chabbishreels1983 Жыл бұрын
What a great video, helps A LOT in the beginning.
@KhadijaHameed-bx2mt
@KhadijaHameed-bx2mt Жыл бұрын
you r going to catch 100k soon :D i noticed it from the 2 days it add +100 on regular but today it +200 so soon 😃👏
@learner5439
@learner5439 Жыл бұрын
Thank you Philipp for this wonderful video.
@maximooze3196
@maximooze3196 Жыл бұрын
send(location), exactly how khabib said it. Thanks again @Philipp
@skarloti
@skarloti Жыл бұрын
Beautiful and clear Thanks!
@AnkitKumar-dd8fo
@AnkitKumar-dd8fo 10 ай бұрын
Adding foregroundServiceType="location" in Manifest file is very crucial for foreground service to run even if app is closed in higher android version.
@elgracko
@elgracko Жыл бұрын
Oh, this worked perfectly, thanks!! you have no idea how much this helps!! I'm definetly buying you that coffee, right now|
@Giulyozz
@Giulyozz Жыл бұрын
hi philip, great video, I was wondering if you had any suggestion for using a default location listener instead of fused location, i need my app to work without play services, thanks
@PrabhatPandey-jk6ur
@PrabhatPandey-jk6ur Жыл бұрын
Thank you brother, you made our life easy.
@dhiraj.20
@dhiraj.20 Жыл бұрын
Nice tutorial, Thanks Philipp
@SengKuz
@SengKuz Жыл бұрын
Nice video, Thank you for your source code. it has useful to my develop about show location.
@mustafaammar551
@mustafaammar551 Жыл бұрын
Very cool video Thank You Bro Wish you all the best
@ChrisAthanas
@ChrisAthanas Жыл бұрын
Very nice and thorough explanation!
@PhilippLackner
@PhilippLackner Жыл бұрын
Thank you!
@McIntoch666
@McIntoch666 Жыл бұрын
Location not updated
@mukammart379
@mukammart379 Жыл бұрын
Hello! Can we unit test for FusedLocationProviderClient using mock? or we have to write ui test for it?
@EmanNollase
@EmanNollase Жыл бұрын
nice tutorial. qq, what is the difference between foreground service vs WorkerManager?
@ahmedelaswly1231
@ahmedelaswly1231 Жыл бұрын
Thank You , it was so clear
@Adam0001
@Adam0001 Жыл бұрын
Thank you Philipp🔥 Could you make a tutorial about How to observe new data in the backgroud and then fire a notificarion. BEST WISHES
@salmaK20012
@salmaK20012 Жыл бұрын
YOU ARE A LIFE SAVER!
@aviksutar
@aviksutar Жыл бұрын
Can you make a tutorial on complex survey form using json. Mean to say that the form is dynamically created from server end json response with multiple different layouts like rating bar, editext, radiobutton, spinners
@juarez.roberto
@juarez.roberto Жыл бұрын
This video is gold !!
@ameentehseen6900
@ameentehseen6900 Жыл бұрын
Finally ! thank you Philipp
@TidelxD
@TidelxD 4 ай бұрын
Thank You Philip you are the best
@jopadjr
@jopadjr 7 ай бұрын
1.3K+...Thanks Philipp. Great tutorial !! Would you mind adding Google Map series in your premium course?
@dalveersinghdaiya2844
@dalveersinghdaiya2844 Жыл бұрын
Exceptional content
@rarespetrusamartean5433
@rarespetrusamartean5433 Жыл бұрын
Thanks man, I got really stumped by needing to declare the service type in the manifest. Everything else was flawless and worked but as soon as I closed the app, it would stop sending locations to my server, which frustrated me to no end to the point where I resorted to youtube. Thanks again, this has been very annoying.
@omarhaddad980
@omarhaddad980 Жыл бұрын
I found that calling `stop()` before even attempting to start will cause a problem. The reason I'm sharing this is because I'm trying to implement a `restartLocationTracking()` feature. It's essentially a location tracking refresher that involves calling `stop()` and then `start()`. This feature is particularly important when developing a Hybrid cross-platform app using Ionic and Capacitor. Is there a way to check if the (location is being tracked) before we call `stop()`?
@erzhananarkulov9826
@erzhananarkulov9826 Жыл бұрын
awesome! I would like to ask what if I want to share location with my backend and see other users location of my app. is it a good if you request to server every second to update location? How uber taxi app handle it, for example? Thanks
@chndraja
@chndraja Жыл бұрын
Hi Philip, I ran into a issue, where my foreground service is closed after sometime in some OEM. Any idea on how to solve this issue.
@berkc5323
@berkc5323 Жыл бұрын
Why did you use launch { send() } instead of trySend()? Is it okay to launch a new coroutine every time that location callback triggers?
@Uz_Mobile_Developer
@Uz_Mobile_Developer Жыл бұрын
Thanks for usefull vedio.
@alirahimi7425
@alirahimi7425 8 күн бұрын
fantastic, but u need to add high accuracy for update frequently with new numbers
@joaopaulopop
@joaopaulopop Жыл бұрын
Thank you very much!
@michaelremme8319
@michaelremme8319 Жыл бұрын
Really really great, what you did. It was exactly what i was searching for ( for nearly a week of search ). Transfer into java was not that complicated. I have one question: why are you building the NorificationChannel inside the Application? I found some others, which are building it inside the Service directly. In that way it seems that they are recreating it with each location update. Cause of this?
@RK-lv9qc
@RK-lv9qc 10 ай бұрын
I dont think it's possible , for creating UI we need to request through mainactivity and we need to build a channel before our activity gets paused or destroyed , so we need to make use of application class , one other way is to use the Mainactivity and build the channel there
@lihan7257
@lihan7257 Жыл бұрын
How to learn your coding style , so beautiful.
@raojilani3103
@raojilani3103 Жыл бұрын
thank you thank you so much for this help
@flowzk921
@flowzk921 Жыл бұрын
Great Video!
@yaddogaming
@yaddogaming Жыл бұрын
How we can make a code editing app or a code compiler (with jetpack compose) , pls make a video on this 🙏
@roshanpvarghese4280
@roshanpvarghese4280 Жыл бұрын
Just wondering why you didn't go with Work manager's long running worker for this?
@EmanNollase
@EmanNollase Жыл бұрын
Also can I make request a tutorial: would possible to make tutorial about swipetodismiss(compose) but the catch is will wait for the server result before it will dismiss or not?
@_WT_WhiteKnight7YT_
@_WT_WhiteKnight7YT_ Жыл бұрын
thank you so much dude you're a god
@panagiotisangelosisiris6315
@panagiotisangelosisiris6315 10 ай бұрын
Hi Phillip! Can I take the coordinates from this foreground service and print them also on screen, somehow?
@xITRicKsHooTzIx
@xITRicKsHooTzIx Жыл бұрын
Hi, another wish I would like to see is background push notifications. I always had problems with that and the implementation is getting harder from experience. Thanks!
@jeromet
@jeromet Жыл бұрын
Check out tutorial using Firebase!
@yugmewada4339
@yugmewada4339 Жыл бұрын
I am working on android kotlin food delivery project i have order tracking screen in which path will be drawn and customer can track the driver’s location application support is 6-12 than what will the best way to do above defined job? I have to send updated lat long to api to suggest me accordingly
@sercanyigit3292
@sercanyigit3292 Жыл бұрын
Hi Philip what theme are you using ?
@siddharthp2061
@siddharthp2061 3 ай бұрын
Nice tutorial ! How are you handling requesting permissions from user ?
@Uz_Mobile_Developer
@Uz_Mobile_Developer Жыл бұрын
Pls can you make vedio about canvas and using 3d objects in android
@onlinemarketing9865
@onlinemarketing9865 Жыл бұрын
Sir,, Please help me.. I have faced a problem.. My app previous update version was only 10 megabyte. But now I have added some features,, the size jumped to 25 MB, which is not possible because I have added very small amount of code... When I cancled all and go to previous version by github using version control; it also showing 25MB in released version,,, which was 10MB..
@bleuforcedz6727
@bleuforcedz6727 Жыл бұрын
Can u do tutorial on how to make a background service cant be killed by battery optimization?
@DrakotMetal
@DrakotMetal Жыл бұрын
why don't you use IntentService? what you include in it runs on the background by default
@danishranjan9208
@danishranjan9208 Жыл бұрын
it work on my pc thx bro vеry much
@cernaruka
@cernaruka 6 ай бұрын
Great, thanks a lot
@jansk598
@jansk598 9 ай бұрын
Hi,does the foreground service run also when device is locked and in sleep mode?
@GirishKumar-vj6yy
@GirishKumar-vj6yy Жыл бұрын
Thanks Bro😊
@xandrviking1113
@xandrviking1113 7 ай бұрын
Super, thank you philip 🖖👍🤝
@abdullahtutun
@abdullahtutun 9 ай бұрын
You haven't used the ACCESS_BACKGROUND_LOCATION permission in the project. Would this cause any issues when uploading the app to Google Play?
@faxmodem2397
@faxmodem2397 Жыл бұрын
Hello, how can we save this information and send it to the backend? And the second question is how can we send an alarm to the backend when the user turns off the GPS
@kiruthigat0152
@kiruthigat0152 10 ай бұрын
Hi philipp,Could you please upload a video on monitor geofence
@deepfuchsia7204
@deepfuchsia7204 Жыл бұрын
Hey Philipp, thanks for the video and the code. I had problems with foreground service and your code worked perfectly fine. I have an additional question tho. Let's say we want to render a map and a polyline which would render something like a path using the list of locations gathered over time. The map is rendered inside a @Composable. What's the best way to pass the location data from the service into the @Composable, so it can render the path?
@hegde421201
@hegde421201 Жыл бұрын
use arcgis polyline API
@berkc5323
@berkc5323 Жыл бұрын
did you find a nice way? I am actually trying to find a way to use the location in a Fragment. I need to pass it to something like a StateFlow and collect it in the Fragment.
@pranjalisrivastava6170
@pranjalisrivastava6170 Жыл бұрын
How to play navigation sound when navigation started to confirm that voice directions are working in android studio?
@rajeevthakur4916
@rajeevthakur4916 6 ай бұрын
I have tried you other videos also and done each step to replicate the same but still having the issue that the notification is easily removeable by just swiping. With the foreground service i want to show a persistent/static/non-removeable notification which includes the stop button to directly stop the foreground service from the notification. By this the user will not have to open the app and the app will not be running in the background when the user dont want it to. I have tried .setOngoing etc but still not solution. Waiting for you response ..........
@galihgaharditama7432
@galihgaharditama7432 Жыл бұрын
Link github of the source code is wrong, can you update it? Thanks..
@luizfelipemontuaniesilva1371
@luizfelipemontuaniesilva1371 6 ай бұрын
Is it possible to set the notification importance to LOW and set it silent without impacting the service?
@Cyber-lw1bi
@Cyber-lw1bi 3 ай бұрын
Hi cool video but how do i make it that the camera of the google map follows the user's GPS position?
@user-ng3kf4cs4d
@user-ng3kf4cs4d 6 ай бұрын
I know i m kinda late but what IDE did you use and what programming languages?
@exploringgodscountry
@exploringgodscountry Ай бұрын
You have the best KZbin channel for Android development for sure. However, I'm old school. I've programmed in a BUNCH of languages... Really a hardware guy though. I cannot wrap my head around Kotlin. Are you not doing any Java tutorials on location services? I honestly, really don't want to learn Kotlin. I don't see it's utility.
@sahilkanabar8605
@sahilkanabar8605 Жыл бұрын
can we track location after user take out our app through recent?
@retechitsolution2564
@retechitsolution2564 10 ай бұрын
Thank its good prectice for me. But one my question How to send location on pho server
@babatundeadenowo7568
@babatundeadenowo7568 5 күн бұрын
I have followed all of the process, but the location in the notification bar remains "Location: null" and doesn't update with the actual latitude and longitude. The only difference in my implementation is that I put the call to start the service in the setContent on the MainActivity, just before the NavHost, rather than onClick of a button. This is because I need my app to always track.
@JonuGurjar
@JonuGurjar 10 ай бұрын
Hi Phillip, I was seeking the same solution but it's not working when app gets killed, can you please add something
@marvelrivera1194
@marvelrivera1194 10 ай бұрын
Nice one
@hossamqandel5303
@hossamqandel5303 Жыл бұрын
Philipp ♥️
@zlash7
@zlash7 Жыл бұрын
The link below "Get the source code for this video here" is wrong
@user-dq7ti9zt4p
@user-dq7ti9zt4p Жыл бұрын
miui kills the foreground service, if battery optimization is enabled and autorun is disabled, I restart the service using FCM, if the device has stopped sending data durin some time, this is not the best solution, but working one
@alirehman842
@alirehman842 Жыл бұрын
How you check service is stopped for sending notification
@EntertheBlackDragon
@EntertheBlackDragon Жыл бұрын
Column will not render, not sure what that's about.
@yugmewada4339
@yugmewada4339 Жыл бұрын
Why you have used flow? Why you cant used mutable live data of Location?
@Tech1st
@Tech1st Жыл бұрын
can't we running after close the app?
@fredericoamigo
@fredericoamigo 3 ай бұрын
Good video, and I'm one of your biggest fans, but unfortunately the user location updates really really slow. Also. Is this a bit too complex? Is there an easier way to do this?
@ManishDubeyAndroid
@ManishDubeyAndroid Жыл бұрын
Can’t we extend LifecycleService and use lifecycleScope instead of creating own coroutineScope with supervisor?
@Salehalanazi-7
@Salehalanazi-7 Жыл бұрын
Yup That's even better :)
@yeinsonjosesernahuamani9542
@yeinsonjosesernahuamani9542 Жыл бұрын
How do I send the location to a node.js server?
@vishwanathkumbar3083
@vishwanathkumbar3083 Жыл бұрын
We have alternative solution for location tracking.we can use listenableworker and show notification
@mohaimensarker
@mohaimensarker Жыл бұрын
Will the location will change with moving the device?
@florianaleman9120
@florianaleman9120 Жыл бұрын
Hey great tutorial. I have some trouble getting the location in background idf i dont implement the code for the notifications. Is it possible ?
@sedraopamp
@sedraopamp Жыл бұрын
I've tested with different devices (Samsung S20, Motorola G42 and Pixel 6a), and LocationManager provides a location every 10 minutes in the background. I've not found an approach to reduce this time. However, you must add background location permission in the manifest and request it to the user. Coarse and Fine location will only work in foreground.
@krepkovmeste
@krepkovmeste Жыл бұрын
It was very informative, but it's not entirely clear where all the code comes from ... I'm beginner in android dev, only know web development and everything is much simpler and clearer there It would be cool if a video appeared on KZbin on how to make a full-fledged gps tracker with sending the received data to the server in MySQL and authorization, or at least there were normal lessons that would teach you how to use the original source to master mobile development...
@dgcov
@dgcov Ай бұрын
I'd be very interested for Philipp to try to replicate the tutorial a year after it was originally posted. So much has been deprecated and changed in the SDK that it no longer compiles.
@sijanneupane
@sijanneupane Жыл бұрын
updated weeks ago, now was the schedule.
@trevortrevose9124
@trevortrevose9124 Жыл бұрын
Heard facebook tracks user search history and keystrokes is this possible or a myth
@Cyber-lw1bi
@Cyber-lw1bi 3 ай бұрын
Hello... Am I the only one with the problem that the gps location does not change? So if i try to set a virtual gps location in extended controls, the app will continue to print out the original location until the whole emulated smartphone has been restarted. And then again, only the one location that has been set before the device restart is continuously printed out...
@jackc-gw7630
@jackc-gw7630 4 ай бұрын
Hey. The location doesnt seem to update continesly anymore?
@rithulraj3348
@rithulraj3348 Жыл бұрын
Is it possible to track location when the app is killed?
@omarhaddad980
@omarhaddad980 Жыл бұрын
at 08:40 I think there must be an OR instead of and AND in the line 24 where you check gps and network
@PhilippLackner
@PhilippLackner Жыл бұрын
No, if one of these is enabled GPS can be used
@omarhaddad980
@omarhaddad980 Жыл бұрын
@@PhilippLackner Great video BTW, and now I get it, so the phone can get location using the network also besides the the Gps sensor itself, in this case I would rename the variable isNetworkEnabled to isNetworkLocationEnabled. but of course this is a personal preference. thanks a lot for the help
@xmaster9841
@xmaster9841 Жыл бұрын
How can i push this data to ViewModel?
@codefriendly8528
@codefriendly8528 Ай бұрын
Is this work on android 14 as well?
@Uz_Mobile_Developer
@Uz_Mobile_Developer Жыл бұрын
Hi Philip
Foreground Services - Android Basics 2023
22:22
Philipp Lackner
Рет қаралды 70 М.
Kotlin Flows in practice
21:06
Android Developers
Рет қаралды 154 М.
HOW DID HE WIN? 😱
00:33
Topper Guild
Рет қаралды 48 МЛН
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 26 МЛН
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 8 МЛН
Should You Use Compose State or StateFlow in Your ViewModels?
13:59
Philipp Lackner
Рет қаралды 72 М.
Track Users Location in the Background with Android and Xamarin.Forms
41:14
WorkManager - Android Basics 2023
34:22
Philipp Lackner
Рет қаралды 51 М.
How NVIDIA just defeated every other tech company
9:20
Mrwhosetheboss
Рет қаралды 834 М.
HOW DID HE WIN? 😱
00:33
Topper Guild
Рет қаралды 48 МЛН