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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 2022
  • 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...

ความคิดเห็น • 172

  • @lifebylazy
    @lifebylazy ปีที่แล้ว +25

    "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

  • @muhammadirfanrajput8400
    @muhammadirfanrajput8400 ปีที่แล้ว +37

    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!

  • @sahilkanabar8605
    @sahilkanabar8605 ปีที่แล้ว +18

    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 .❤

  • @juansuarez5216
    @juansuarez5216 ปีที่แล้ว +2

    Congrats!! Really good tutorial, well explained and keeping it as simple as possible

  • @BapeRuLLZ
    @BapeRuLLZ ปีที่แล้ว +6

    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.

  • @chabbishreels1983
    @chabbishreels1983 ปีที่แล้ว

    What a great video, helps A LOT in the beginning.

  • @halvtysk
    @halvtysk ปีที่แล้ว +3

    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.

  • @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.

  • @skarloti
    @skarloti ปีที่แล้ว

    Beautiful and clear Thanks!

  • @maximooze3196
    @maximooze3196 ปีที่แล้ว +1

    send(location), exactly how khabib said it. Thanks again @Philipp

  • @elgracko
    @elgracko ปีที่แล้ว

    Oh, this worked perfectly, thanks!! you have no idea how much this helps!!
    I'm definetly buying you that coffee, right now|

  • @dhiraj.20
    @dhiraj.20 ปีที่แล้ว

    Nice tutorial, Thanks Philipp

  • @PrabhatPandey-jk6ur
    @PrabhatPandey-jk6ur ปีที่แล้ว

    Thank you brother, you made our life easy.

  • @mustafaammar551
    @mustafaammar551 ปีที่แล้ว

    Very cool video
    Thank You Bro
    Wish you all the best

  • @mukammart379
    @mukammart379 ปีที่แล้ว +4

    Hello! Can we unit test for FusedLocationProviderClient using mock? or we have to write ui test for it?

  • @SengKuz
    @SengKuz ปีที่แล้ว

    Nice video, Thank you for your source code. it has useful to my develop about show location.

  • @juarez.roberto
    @juarez.roberto ปีที่แล้ว

    This video is gold !!

  • @salmaK20012
    @salmaK20012 ปีที่แล้ว +1

    YOU ARE A LIFE SAVER!

  • @ahmedelaswly1231
    @ahmedelaswly1231 ปีที่แล้ว

    Thank You , it was so clear

  • @TidelxD
    @TidelxD 4 หลายเดือนก่อน

    Thank You Philip you are the best

  • @dalveersinghdaiya2844
    @dalveersinghdaiya2844 ปีที่แล้ว +1

    Exceptional content

  • @ChrisAthanas
    @ChrisAthanas ปีที่แล้ว +2

    Very nice and thorough explanation!

  • @McIntoch666
    @McIntoch666 ปีที่แล้ว +6

    Location not updated

  • @AnkitKumar-dd8fo
    @AnkitKumar-dd8fo 10 หลายเดือนก่อน +2

    Adding foregroundServiceType="location" in Manifest file is very crucial for foreground service to run even if app is closed in higher android version.

  • @Giulyozz
    @Giulyozz ปีที่แล้ว +1

    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

  • @ameentehseen6900
    @ameentehseen6900 ปีที่แล้ว

    Finally ! thank you Philipp

  • @flowzk921
    @flowzk921 ปีที่แล้ว

    Great Video!

  • @chndraja
    @chndraja ปีที่แล้ว +1

    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.

  • @EmanNollase
    @EmanNollase ปีที่แล้ว +2

    nice tutorial. qq, what is the difference between foreground service vs WorkerManager?

  • @Uz_Mobile_Developer
    @Uz_Mobile_Developer ปีที่แล้ว +1

    Thanks for usefull vedio.

  • @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?

  • @Adam0001
    @Adam0001 ปีที่แล้ว +1

    Thank you Philipp🔥
    Could you make a tutorial about How to observe new data in the backgroud and then fire a notificarion.
    BEST WISHES

  • @jansk598
    @jansk598 10 หลายเดือนก่อน +1

    Hi,does the foreground service run also when device is locked and in sleep mode?

  • @joaopaulopop
    @joaopaulopop ปีที่แล้ว

    Thank you very much!

  • @sercanyigit3292
    @sercanyigit3292 ปีที่แล้ว +1

    Hi Philip what theme are you using ?

  • @xandrviking1113
    @xandrviking1113 7 หลายเดือนก่อน

    Super, thank you philip 🖖👍🤝

  • @raojilani3103
    @raojilani3103 ปีที่แล้ว

    thank you thank you so much for this help

  • @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

  • @jopadjr
    @jopadjr 8 หลายเดือนก่อน

    1.3K+...Thanks Philipp. Great tutorial !! Would you mind adding Google Map series in your premium course?

  • @cernaruka
    @cernaruka 7 หลายเดือนก่อน

    Great, thanks a lot

  • @_WT_WhiteKnight7YT_
    @_WT_WhiteKnight7YT_ ปีที่แล้ว

    thank you so much dude you're a god

  • @panagiotisangelosisiris6315
    @panagiotisangelosisiris6315 11 หลายเดือนก่อน

    Hi Phillip! Can I take the coordinates from this foreground service and print them also on screen, somehow?

  • @GirishKumar-vj6yy
    @GirishKumar-vj6yy ปีที่แล้ว +1

    Thanks Bro😊

  • @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()`?

  • @roshanpvarghese4280
    @roshanpvarghese4280 ปีที่แล้ว +1

    Just wondering why you didn't go with Work manager's long running worker for this?

  • @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

  • @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.

  • @lihan7257
    @lihan7257 ปีที่แล้ว

    How to learn your coding style , so beautiful.

  • @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?

  • @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..

  • @alirahimi7425
    @alirahimi7425 24 วันที่ผ่านมา

    fantastic, but u need to add high accuracy for update frequently with new numbers

  • @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

  • @marvelrivera1194
    @marvelrivera1194 11 หลายเดือนก่อน

    Nice one

  • @danishranjan9208
    @danishranjan9208 ปีที่แล้ว

    it work on my pc thx bro vеry much

  • @user-ng3kf4cs4d
    @user-ng3kf4cs4d 7 หลายเดือนก่อน

    I know i m kinda late but what IDE did you use and what programming languages?

  • @rajeevthakur4916
    @rajeevthakur4916 7 หลายเดือนก่อน

    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 ..........

  • @DrakotMetal
    @DrakotMetal ปีที่แล้ว

    why don't you use IntentService? what you include in it runs on the background by default

  • @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?

  • @galihgaharditama7432
    @galihgaharditama7432 ปีที่แล้ว +1

    Link github of the source code is wrong, can you update it? Thanks..

  • @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

  • @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

  • @pranjalisrivastava6170
    @pranjalisrivastava6170 ปีที่แล้ว

    How to play navigation sound when navigation started to confirm that voice directions are working in android studio?

  • @siddharthp2061
    @siddharthp2061 3 หลายเดือนก่อน

    Nice tutorial ! How are you handling requesting permissions from user ?

  • @sahilkanabar8605
    @sahilkanabar8605 ปีที่แล้ว

    can we track location after user take out our app through recent?

  • @hossamqandel5303
    @hossamqandel5303 ปีที่แล้ว

    Philipp ♥️

  • @luizfelipemontuaniesilva1371
    @luizfelipemontuaniesilva1371 7 หลายเดือนก่อน

    Is it possible to set the notification importance to LOW and set it silent without impacting the service?

  • @yaddogaming
    @yaddogaming ปีที่แล้ว

    How we can make a code editing app or a code compiler (with jetpack compose) , pls make a video on this 🙏

  • @fredericoamigo
    @fredericoamigo 4 หลายเดือนก่อน

    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?

  • @bleuforcedz6727
    @bleuforcedz6727 ปีที่แล้ว +1

    Can u do tutorial on how to make a background service cant be killed by battery optimization?

  • @xITRicKsHooTzIx
    @xITRicKsHooTzIx ปีที่แล้ว +1

    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!

  • @exploringgodscountry
    @exploringgodscountry 2 หลายเดือนก่อน

    You have the best TH-cam 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.

  • @JonuGurjar
    @JonuGurjar 11 หลายเดือนก่อน

    Hi Phillip, I was seeking the same solution but it's not working when app gets killed, can you please add something

  • @codefriendly8528
    @codefriendly8528 2 หลายเดือนก่อน

    Is this work on android 14 as well?

  • @Uz_Mobile_Developer
    @Uz_Mobile_Developer ปีที่แล้ว +1

    Hi Philip

  • @Uz_Mobile_Developer
    @Uz_Mobile_Developer ปีที่แล้ว +2

    Pls can you make vedio about canvas and using 3d objects in android

  • @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.

  • @zlash7
    @zlash7 ปีที่แล้ว

    The link below "Get the source code for this video here" is wrong

  • @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?

  • @Tech1st
    @Tech1st ปีที่แล้ว

    can't we running after close the app?

  • @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 5 หลายเดือนก่อน

    Hey. The location doesnt seem to update continesly anymore?

  • @user-dq7ti9zt4p
    @user-dq7ti9zt4p ปีที่แล้ว +2

    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

  • @yugmewada4339
    @yugmewada4339 ปีที่แล้ว

    Why you have used flow? Why you cant used mutable live data of Location?

  • @kiruthigat0152
    @kiruthigat0152 11 หลายเดือนก่อน

    Hi philipp,Could you please upload a video on monitor geofence

  • @hydernotes
    @hydernotes 5 หลายเดือนก่อน

    Will it keep running it I terminate the app?

  • @babatundeadenowo7568
    @babatundeadenowo7568 22 วันที่ผ่านมา

    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.

  • @ManishDubeyAndroid
    @ManishDubeyAndroid ปีที่แล้ว

    Can’t we extend LifecycleService and use lifecycleScope instead of creating own coroutineScope with supervisor?

  • @EntertheBlackDragon
    @EntertheBlackDragon ปีที่แล้ว

    Column will not render, not sure what that's about.

  • @yeinsonjosesernahuamani9542
    @yeinsonjosesernahuamani9542 ปีที่แล้ว

    How do I send the location to a node.js server?

  • @rithulraj3348
    @rithulraj3348 ปีที่แล้ว

    Is it possible to track location when the app is killed?

  • @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.

  • @retechitsolution2564
    @retechitsolution2564 10 หลายเดือนก่อน

    Thank its good prectice for me.
    But one my question How to send location on pho server

  • @trevortrevose9124
    @trevortrevose9124 ปีที่แล้ว

    Heard facebook tracks user search history and keystrokes is this possible or a myth

  • @xmaster9841
    @xmaster9841 ปีที่แล้ว

    How can i push this data to ViewModel?

  • @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 TH-cam 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 2 หลายเดือนก่อน

    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.

  • @RadhaVaddireddy
    @RadhaVaddireddy ปีที่แล้ว

    showing 'create(): LocationRequest' is deprecated. Deprecated in Java' message, please suggest how to resolve this issue.

  • @avinashgupta4038
    @avinashgupta4038 8 หลายเดือนก่อน

    the code not working, can you please make another video on this topic

  • @mondoshigua
    @mondoshigua ปีที่แล้ว

    🇨🇴🧔🏻👍🏼🤝🏼 Saludos desde Colombia.