Тёмный

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

Philipp Lackner
Подписаться 186 тыс.
Просмотров 79 тыс.
50% 1

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/...
💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
pl-coding.com/...
Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
pl-coding.com/...
Join this channel to get access to perks:
/ @philipplackner
Get the source code for this video here:
github.com/phi...
Join my Discord server:
/ discord
Regular programming advice on my Instagram page: / _philipplackner_
Checkout my GitHub: github.com/phi...
You like my free content? Here you can buy me a coffee:
www.buymeacoff...

Опубликовано:

 

21 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 172   
@sahilkanabar8605
@sahilkanabar8605 2 года назад
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 2 года назад
friend how would it be sent to a node.js server?
@sahilkanabar8605
@sahilkanabar8605 2 года назад
@@yeinsonjosesernahuamani9542 sorry i could not understand you.
@yeinsonjosesernahuamani9542
@yeinsonjosesernahuamani9542 2 года назад
@@sahilkanabar8605 How do I send the location to a server in node.js?
@โอตาคุโง่ๆคนหนึ่ง
@@yeinsonjosesernahuamani9542 use retrofit?
@muhammadirfanrajput8400
@muhammadirfanrajput8400 2 года назад
Android gives continuous pain by continuous changing android SDK
@ChrisAthanas
@ChrisAthanas 2 года назад
Its a constant clusterfuck for sure
@SEMPERFIDELIS7
@SEMPERFIDELIS7 Год назад
It sure is one hell of a shame!
@lifebylazy
@lifebylazy 2 года назад
"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
@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.
@AnkitKumar-dd8fo
@AnkitKumar-dd8fo Год назад
Adding foregroundServiceType="location" in Manifest file is very crucial for foreground service to run even if app is closed in higher android version.
@juansuarez5216
@juansuarez5216 Год назад
Congrats!! Really good tutorial, well explained and keeping it as simple as possible
@halvtysk
@halvtysk 2 года назад
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.
@maximooze3196
@maximooze3196 2 года назад
send(location), exactly how khabib said it. Thanks again @Philipp
@ChrisAthanas
@ChrisAthanas 2 года назад
Very nice and thorough explanation!
@PhilippLackner
@PhilippLackner 2 года назад
Thank you!
@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 😃👏
@mukammart379
@mukammart379 2 года назад
Hello! Can we unit test for FusedLocationProviderClient using mock? or we have to write ui test for it?
@alirahimi7425
@alirahimi7425 3 месяца назад
fantastic, but u need to add high accuracy for update frequently with new numbers
@EmanNollase
@EmanNollase 2 года назад
nice tutorial. qq, what is the difference between foreground service vs WorkerManager?
@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 2 года назад
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
@chabbishreels1983
@chabbishreels1983 2 года назад
What a great video, helps A LOT in the beginning.
@guixp2965
@guixp2965 14 дней назад
I love u bro, saved my life
@exploringgodscountry
@exploringgodscountry 4 месяца назад
You have the best RU-vid 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.
@jopadjr
@jopadjr 11 месяцев назад
1.3K+...Thanks Philipp. Great tutorial !! Would you mind adding Google Map series in your premium course?
@learner5439
@learner5439 Год назад
Thank you Philipp for this wonderful video.
@salmaK20012
@salmaK20012 Год назад
YOU ARE A LIFE SAVER!
@siddharthp2061
@siddharthp2061 6 месяцев назад
Nice tutorial ! How are you handling requesting permissions from user ?
@sercanyigit3292
@sercanyigit3292 2 года назад
Hi Philip what theme are you using ?
@Adam0001
@Adam0001 2 года назад
Thank you Philipp🔥 Could you make a tutorial about How to observe new data in the backgroud and then fire a notificarion. BEST WISHES
@skarloti
@skarloti 2 года назад
Beautiful and clear Thanks!
@McIntoch666
@McIntoch666 2 года назад
Location not updated
@fredericoamigo
@fredericoamigo 7 месяцев назад
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?
@dhiraj.20
@dhiraj.20 2 года назад
Nice tutorial, Thanks Philipp
@PrabhatPandey-jk6ur
@PrabhatPandey-jk6ur Год назад
Thank you brother, you made our life easy.
@mustafaammar551
@mustafaammar551 2 года назад
Very cool video Thank You Bro Wish you all the best
@TidelxD
@TidelxD 7 месяцев назад
Thank You Philip you are the best
@SengKuz
@SengKuz Год назад
Nice video, Thank you for your source code. it has useful to my develop about show location.
@erzhananarkulov9826
@erzhananarkulov9826 2 года назад
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
@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.
@Uz_Mobile_Developer
@Uz_Mobile_Developer 2 года назад
Thanks for usefull vedio.
@dalveersinghdaiya2844
@dalveersinghdaiya2844 2 года назад
Exceptional content
@ahmedelaswly1231
@ahmedelaswly1231 Год назад
Thank You , it was so clear
@juarez.roberto
@juarez.roberto 2 года назад
This video is gold !!
@roshanpvarghese4280
@roshanpvarghese4280 2 года назад
Just wondering why you didn't go with Work manager's long running worker for this?
@xandrviking1113
@xandrviking1113 10 месяцев назад
Super, thank you philip 🖖👍🤝
@InputOutput-b2l
@InputOutput-b2l 9 месяцев назад
I know i m kinda late but what IDE did you use and what programming languages?
@Cyber-lw1bi
@Cyber-lw1bi 6 месяцев назад
Hi cool video but how do i make it that the camera of the google map follows the user's GPS position?
@raojilani3103
@raojilani3103 Год назад
thank you thank you so much for this help
@ameentehseen6900
@ameentehseen6900 2 года назад
Finally ! thank you Philipp
@jansk598
@jansk598 Год назад
Hi,does the foreground service run also when device is locked and in sleep mode?
@lihan7257
@lihan7257 2 года назад
How to learn your coding style , so beautiful.
@chndraja
@chndraja 2 года назад
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.
@Uz_Mobile_Developer
@Uz_Mobile_Developer 2 года назад
Pls can you make vedio about canvas and using 3d objects in android
@joaopaulopop
@joaopaulopop Год назад
Thank you very much!
@kiruthigat0152
@kiruthigat0152 Год назад
Hi philipp,Could you please upload a video on monitor geofence
@aviksutar
@aviksutar 2 года назад
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
@panagiotisangelosisiris6315
Hi Phillip! Can I take the coordinates from this foreground service and print them also on screen, somehow?
@GirishKumar-vj6yy
@GirishKumar-vj6yy Год назад
Thanks Bro😊
@xITRicKsHooTzIx
@xITRicKsHooTzIx 2 года назад
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 2 года назад
Check out tutorial using Firebase!
@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?
@_WT_WhiteKnight7YT_
@_WT_WhiteKnight7YT_ 2 года назад
thank you so much dude you're a god
@bleuforcedz6727
@bleuforcedz6727 2 года назад
Can u do tutorial on how to make a background service cant be killed by battery optimization?
@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 Год назад
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
@АзизбекАширалиев-щ6ъ
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 2 года назад
How you check service is stopped for sending notification
@cernaruka
@cernaruka 10 месяцев назад
Great, thanks a lot
@flowzk921
@flowzk921 2 года назад
Great Video!
@retechitsolution2564
@retechitsolution2564 Год назад
Thank its good prectice for me. But one my question How to send location on pho server
@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()`?
@rajeevthakur4916
@rajeevthakur4916 9 месяцев назад
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 ..........
@danishranjan9208
@danishranjan9208 2 года назад
it work on my pc thx bro vеry much
@dgcov
@dgcov 4 месяца назад
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.
@EmanNollase
@EmanNollase 2 года назад
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?
@yaddogaming
@yaddogaming 2 года назад
How we can make a code editing app or a code compiler (with jetpack compose) , pls make a video on this 🙏
@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
@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
@abdullahtutun
@abdullahtutun Год назад
You haven't used the ACCESS_BACKGROUND_LOCATION permission in the project. Would this cause any issues when uploading the app to Google Play?
@Cyber-lw1bi
@Cyber-lw1bi 6 месяцев назад
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...
@roshkaalex
@roshkaalex Год назад
Hi Philipp. What about ACCESS_BACKGROUND_LOCATION permission? Should we use this permission for tracking in background?
@NathanMeadeWSG
@NathanMeadeWSG Год назад
I had to. Also for Android 13 devices you will need to prompt the user to "Allow all the time" for the location permissions of the app.
@sedraopamp
@sedraopamp Год назад
Fine and coarse locations will only work in foreground since Android 11. Access_Background_Location is a must BUT these android versions reduce location updates to report them every 10 minutes in my tests with different manufacturers in the background.
@roshkaalex
@roshkaalex Год назад
@@sedraopamp we had the same results... i thought that this permission will resolve background tracking issues... but still we have empty zone on the map
@sedraopamp
@sedraopamp Год назад
@@roshkaalex You need also to disable battery optimization e.g. Samsung S20 stops the location manager if that feature is enabled, no matter if background location permission is allowed.
@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.
@galihgaharditama7432
@galihgaharditama7432 2 года назад
Link github of the source code is wrong, can you update it? Thanks..
@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
@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.
@codefriendly8528
@codefriendly8528 4 месяца назад
Is this work on android 14 as well?
@DrakotMetal
@DrakotMetal 2 года назад
why don't you use IntentService? what you include in it runs on the background by default
@ManishDubeyAndroid
@ManishDubeyAndroid 2 года назад
Can’t we extend LifecycleService and use lifecycleScope instead of creating own coroutineScope with supervisor?
@Salehalanazi-7
@Salehalanazi-7 2 года назад
Yup That's even better :)
@Internet_Ghost-GA
@Internet_Ghost-GA Год назад
in 4:41 the android studio gives me an error . FusedLocationProviderClient . it gives me red color on these line I don't know how to solve these I did exactly like you .
@yugmewada4339
@yugmewada4339 Год назад
Why you have used flow? Why you cant used mutable live data of Location?
@JonuGurjar
@JonuGurjar Год назад
Hi Phillip, I was seeking the same solution but it's not working when app gets killed, can you please add something
@onlinemarketing9865
@onlinemarketing9865 2 года назад
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..
@pranjalisrivastava6170
@pranjalisrivastava6170 Год назад
How to play navigation sound when navigation started to confirm that voice directions are working in android studio?
@Uz_Mobile_Developer
@Uz_Mobile_Developer 2 года назад
Hi Philip
@marvelrivera1194
@marvelrivera1194 Год назад
Nice one
@babatundeadenowo7568
@babatundeadenowo7568 3 месяца назад
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.
@adilansari-bm5vb
@adilansari-bm5vb 2 дня назад
i also facing same issue how you fix this issue????
@jackc-gw7630
@jackc-gw7630 8 месяцев назад
Hey. The location doesnt seem to update continesly anymore?
@sahilkanabar8605
@sahilkanabar8605 2 года назад
can we track location after user take out our app through recent?
@zlash7
@zlash7 2 года назад
The link below "Get the source code for this video here" is wrong
@avinashgupta4038
@avinashgupta4038 11 месяцев назад
the code not working, can you please make another video on this topic
@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 RU-vid 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...
@Tech1st
@Tech1st 2 года назад
can't we running after close the app?
@EntertheBlackDragon
@EntertheBlackDragon Год назад
Column will not render, not sure what that's about.
@vishwanathkumbar3083
@vishwanathkumbar3083 2 года назад
We have alternative solution for location tracking.we can use listenableworker and show notification
@additionalaccount4974
@additionalaccount4974 Год назад
everything is fine but the location is hella inaccurate. i need very precise location please help
@hydernotes
@hydernotes 7 месяцев назад
Will it keep running it I terminate the app?
@xmaster9841
@xmaster9841 Год назад
How can i push this data to ViewModel?
@rithulraj3348
@rithulraj3348 2 года назад
Is it possible to track location when the app is killed?
Далее
Compose Animations in Kotlin Multiplatform
10:37
skibidi toilet multiverse 042
20:57
Просмотров 4,9 млн
Foreground Services - Android Basics 2023
22:22
Просмотров 83 тыс.
Bottom Nav Bar #03 | Material 3 | Jetpack Compose
12:21
WorkManager - Android Basics 2023
34:22
Просмотров 59 тыс.