Understanding and Using Services in Android: Background & Foreground Services

Sdílet
Vložit
  • čas přidán 5. 07. 2024
  • If you find this video helpful, please Like, Share, and Subscribe to support the channel!
    Download project: codeible.com/coursefiles/andr...
    View article on Codeible.com: codeible.com/view/videotutori...
    Hello, in this video, I will show you how to utilize Background and Foreground Services in Android.
    There are 3 types of services in Android - Background, Foreground, and Bound. Each of these terms are misleading because it is not describing the behavior of how each service are used, but it is describing how they are terminated.
    For Android Developers, a Service is a component that runs on the background to perform long-running tasks.
    A Background Service is a service that runs only when the app is running so it’ll get terminated when the app is terminated.
    A Foreground Service is a service that stays alive even when the app is terminated.
    And a Bound Service is a service that runs only if the component it is bound to is still active.
    0:00 - Start of video
    0:12 - Background, Foreground, and Bound Services
    0:50 - Creating and Demoing Background Services
    3:42 - Creating and Demoing Foreground Services
    11:12 - Restarting a Foreground Service on Device Reboot using BroadcastReceiver
    Support Codeible on Patreon!
    / codeible
    Reddit:
    / codeible
    Follow on Pinterest:
    / codeible
    Follow on Instagram:
    / codeibleig
    Follow on Twitter:
    intent/follow?ori...

Komentáře • 111

  • @roman71717
    @roman71717 Před 2 lety +5

    This is one of the best-explained tutorials I have watched!!
    Thank you :)

  • @teladoum9
    @teladoum9 Před rokem

    Thank you. I'm looking for this video longtime.
    Be blessed. Soyez béni mon frère.

  • @DebugWithAdam
    @DebugWithAdam Před 2 lety +8

    I have been looking for something like this to complete our 5 months flutter project, finally I got the solution from your content. Thank you so much, looking forward to seeing more contents

  • @itallstartedwhen
    @itallstartedwhen Před 2 lety +5

    Best video on the topic on CZcams, so well explained 😍😍😍😍😍😍😍😍

  • @truthonly_
    @truthonly_ Před 2 lety +5

    Ultimate content. I never understood services that easily ever before. Thank you 🤟

  • @satwikkar7761
    @satwikkar7761 Před 2 lety +1

    Best video on utube regarding services in android... 👍👍👍👍

  • @lubuntumleto5052
    @lubuntumleto5052 Před rokem +3

    Good example how simple it could be, when guide is so clear and opened step by step, thank you sir.

  •  Před 2 lety +4

    Thank you for clear and worked practice even on API 28. Great work. More android tutorials please.

  • @strawberryinc.2275
    @strawberryinc.2275 Před rokem +3

    i really needed this, @Codeible, thank you big time.

  • @frax84
    @frax84 Před 2 lety +28

    I give you 3C score for this video: Concise, Complete, Clear. Really good explanation. Thank you

  • @drakohgaming5131
    @drakohgaming5131 Před 6 měsíci

    you're salve my life bro, i have weeks searching this info. Thanksh! like and new sub

  • @involved1984
    @involved1984 Před 8 měsíci +1

    Amazing and simple explaination. Thanks. Keep up the good work 👍

  • @ravindrashekhar4419
    @ravindrashekhar4419 Před rokem +1

    Clear Cut simple and Illustrative video, good for starters.

  • @onwodoh
    @onwodoh Před rokem

    This was a good video. I learned so much in a short amount of time!

  • @abunaser3525
    @abunaser3525 Před rokem +1

    I left java 3 years ago .and start using kotlin . But your explanation is So good that I end up subscribing to your channel. Hope you start using compose multiplatform 🙂 this is the very first video from you that I watched .so don't you if you are already using kotlin . Anyway nice video ..

  • @joshuantw
    @joshuantw Před 2 lety +1

    Awesome video my dude. Really clear.

  • @hassanesilverlight1550
    @hassanesilverlight1550 Před 2 lety +1

    Thank you. Best Service tutorial ever.

    • @mohdfaris9054
      @mohdfaris9054 Před rokem

      Terbaik servies dia..tapi home Macan setan dia...malu.out site..setan bertanduk lima..celaka

  • @okeuwechue9238
    @okeuwechue9238 Před rokem +4

    Very clear and direct explanation of the different service types. 👍
    However you mention that multiple instances of the service will get created if started multiple times, but this isn't so; there will only be one instance of the service object but its onStartCommand() function will be invoked those multiple times.

  • @georgenady7375
    @georgenady7375 Před 2 lety +1

    man ! thank you very much for your awsom explaination for that topic

  • @asimjmi2010
    @asimjmi2010 Před 2 lety +1

    Nice explanation.... you're awesome man 👍🏻

  • @Somerandomnessvvv
    @Somerandomnessvvv Před 2 lety +4

    Come onnn this gotta be one of the best practical service tuts. Can’t wait to see you work with kotlin.

    • @mohdfaris9054
      @mohdfaris9054 Před rokem

      Yesss good job baby..setan kocak bini orang sampai dsana ..party sex u setan...

  • @myhealthobs5290
    @myhealthobs5290 Před rokem +1

    I wonder who organised the naming if foreground vs background. Unbelievable, thanks for clearing it up

  • @MauricioSouza-93
    @MauricioSouza-93 Před 2 měsíci

    Great explanation!! Thanks!!

  • @petermolnar6017
    @petermolnar6017 Před 7 měsíci

    Thanks for this great video!

  • @shubhm_agarwal
    @shubhm_agarwal Před 2 lety +1

    Great explanation helped me a lot.
    Hence subscribed.

  • @TupolevSlaVTuber
    @TupolevSlaVTuber Před rokem +1

    nice tut crystal clear and spot on

  • @explorer1125
    @explorer1125 Před rokem +1

    Very good explanation..!!👏

  • @codingfeed9206
    @codingfeed9206 Před 2 lety +1

    Im your new Subscriber because of your video i easily understand some coding stuff

  • @suche_cz
    @suche_cz Před 2 lety +1

    Thank you, this video was really usefull

  • @vr_connoisseur
    @vr_connoisseur Před rokem +1

    nice video, helped me so much!

  • @alexmercerind
    @alexmercerind Před 6 měsíci

    Very useful!!!

  • @kemaldeveli8195
    @kemaldeveli8195 Před 2 lety +1

    I like your videos a lot please share more videos with us

  • @maliksoft3675
    @maliksoft3675 Před 2 lety +1

    very nice thanks for this video.

  • @yusufmustari
    @yusufmustari Před 2 lety

    Thanks bro... very helpfull

  • @yacoubasylla7358
    @yacoubasylla7358 Před 2 lety +1

    Thank you very much

  • @LasWegas
    @LasWegas Před 11 měsíci +2

    Great!

  • @hrishikeshpator3853
    @hrishikeshpator3853 Před rokem +1

    Thank you❤

  • @user-up7zn7sn4f
    @user-up7zn7sn4f Před 2 lety +1

    I hope to learn how to play the list view of music through the service by Raw or Assest folder to work in background long time ... thanks for this video

  • @mdrashid6002
    @mdrashid6002 Před měsícem

    Great Thanks boss for such a clear explanation,
    What about the Bound Services??

  • @NielAclag
    @NielAclag Před 2 lety +1

    thank you!!

  • @NghiaNguyen-hn5st
    @NghiaNguyen-hn5st Před 2 lety +1

    its amazingg thanks you

  • @androidlearners9163
    @androidlearners9163 Před 2 lety +1

    Thanks alot bro

  • @AshooOffice
    @AshooOffice Před rokem +1

    Thnks a lot

  • @mediaatech7992
    @mediaatech7992 Před 2 lety

    so thanks you bro

  • @marwenhammami9575
    @marwenhammami9575 Před rokem +1

    Thanks

  • @Hanna-ir6te
    @Hanna-ir6te Před 2 lety

    Thanks for this tutorial. May I know how how to create second application which will run in the background service when first application is processing without interacting of first app?

  • @avigneswaranwaran
    @avigneswaranwaran Před rokem +1

    Awesome

  • @Hanna-zj1gk
    @Hanna-zj1gk Před 2 lety

    This tutorial is very helpful. May I know how how to create second application which will run in the background service when first application is processing without interacting of first app?

  • @hometv8446
    @hometv8446 Před 2 lety

    Very helpful

  • @sandeepchauhan7918
    @sandeepchauhan7918 Před 2 lety

    Very good explanation.

    • @sandeepchauhan7918
      @sandeepchauhan7918 Před 2 lety

      @instagram I cant answer It. I have limited knowledge and experiance.

  • @llothar68
    @llothar68 Před 7 měsíci

    If i use NDK to run a C++ library, which service should i use to run the code out of process? Sometimes C++ crashs and i want them to take a service down but not the whole app. So i need to do the same as all webbrowsers do it.

  • @sedatdemirtas9955
    @sedatdemirtas9955 Před 2 lety +1

    Thanks

  • @adnananik7586
    @adnananik7586 Před 11 měsíci

    thanks

  • @aymaneme96
    @aymaneme96 Před 2 lety +1

    waaaaaaaaaaaaaaaaaaaaaaaaaw ma bro its amazing

  • @virgile8453
    @virgile8453 Před 2 lety +1

    amazing

  • @derkeks814
    @derkeks814 Před 2 lety

    Great Video! Is there a way to stop the Service by code?

  • @Hong-Mu
    @Hong-Mu Před 2 lety

    useful

  • @habibmalim6178
    @habibmalim6178 Před 2 lety +1

    so helpful thank you very much 😊
    does it work with all android versions specially oreo or higher ?

    • @Codeible
      @Codeible  Před 2 lety +1

      Hello it should work for API 26 or higher.

    • @hamsakumar4535
      @hamsakumar4535 Před 2 lety

      @@Codeible Any alternate service to run for below 26.

  • @alokeshnath4269
    @alokeshnath4269 Před 2 lety +2

    I have tested this on real device when I restart the device I didn't see the notification that you got...? Is there anything to add more?

  • @jeremyheng8573
    @jeremyheng8573 Před 2 lety

    Thank you for the sharing! Any idea we can get Firestore document update if our app is terminated?

    • @Codeible
      @Codeible  Před 2 lety

      Hello,
      Yes you can.

    • @jeremyheng8573
      @jeremyheng8573 Před 2 lety

      @@Codeiblewhat method should we implement to update client document if app is terminated

    • @Codeible
      @Codeible  Před 2 lety

      @@jeremyheng8573 What do you mean? You can just update the document like normal using the Firebase API for Android.

    • @mohdfaris9054
      @mohdfaris9054 Před rokem

      Hello good job.....nerka u all setan...nice...tqqqq

  • @musicjagot1632
    @musicjagot1632 Před 9 měsíci

    Android 12 & 13 How long will it be running Foreground service??

  • @alisheykhi9649
    @alisheykhi9649 Před rokem

    great bro , but im looking for Bound service impl 🙂

  • @moe_shah_0078
    @moe_shah_0078 Před 11 měsíci

    How would you stop the foreground service?

  • @lucasmontec
    @lucasmontec Před rokem

    I believe from what I read on google documentation, that there is no need to check if the service is running. Apparently, that was a bug.

  • @AnuragSingh-eb6of
    @AnuragSingh-eb6of Před rokem

    How I can block wifi internet from using to a specific app in aops /os

  • @mychannel-jb3zk
    @mychannel-jb3zk Před rokem

    I understand that telegram had an unlimited open service in background, how it is posible?

  • @cloner_8852
    @cloner_8852 Před 2 lety

    I have created an alarm app in Android 11, but it does not work when the app is closed. Does anyone know where the problem is ??

  • @67Diadem
    @67Diadem Před 2 lety

    After restarting the app background service is not displaying in the notification area. I have tested on my mobile. Please help 🥺

  • @gunturdarmawan1513
    @gunturdarmawan1513 Před 2 lety +1

    U have udemy courses?

  • @slcsb116
    @slcsb116 Před rokem

    Can you make this in kotlin ?

  • @pewienktos1292
    @pewienktos1292 Před 2 lety

    n1

  • @cosovic14
    @cosovic14 Před 2 lety

    Is there a way to have a foreground service without a notification ?

  • @tanmaysinghal8370
    @tanmaysinghal8370 Před 2 lety

    For me only one notification was being created even without checking if the service is running already or not....

  • @harleenkaurarora1817
    @harleenkaurarora1817 Před 2 lety

    If phone is idle mode background service is not working. How to do this??

    • @Codeible
      @Codeible  Před 2 lety

      You need to use foreground service.

    • @harleenarora5189
      @harleenarora5189 Před 2 lety

      @@Codeible Example you have provided is with foreground service only right?

    • @Codeible
      @Codeible  Před 2 lety

      @@harleenarora5189 Both Foreground and Background.

  • @jaimitkumarpanchal7603
    @jaimitkumarpanchal7603 Před rokem +1

    if service stops after few seconds then what to do?

    • @Codeible
      @Codeible  Před rokem

      If the service stops, it could mean that the task is finished. If you want to keep it alive, you need to do something to keep the service alive like a while loop.

  • @ArturoDevA
    @ArturoDevA Před 2 lety

    fail when stop service and after 10 seconds enable

  • @gianmarioorru1808
    @gianmarioorru1808 Před 2 lety

    the app works fine in the background, but if I want to use it in the foreground it crashes after about 7 seconds. I don't know why

    • @onuralagoz6531
      @onuralagoz6531 Před rokem

      Use wake lock with no time limit to make foreground service work infinitely. But never forget that unnecessary amount of foreground service usage is a real battery killer

  • @pankajlagad3291
    @pankajlagad3291 Před 2 lety

    How to keep running the service even we kill the app ?

    • @Codeible
      @Codeible  Před 2 lety +2

      Create a foreground service - 3:43
      If you want to restart the service automatically, create a broadcast receiver - 11:12

  • @priyanshusahu8024
    @priyanshusahu8024 Před 2 lety

    i cant able to receive broadcast after my app has been killed

  • @nikhilsachan_cse7144
    @nikhilsachan_cse7144 Před rokem

    There is no code for stopping foreground service

  • @Nick-cx4rs
    @Nick-cx4rs Před 11 měsíci

    Yo how this receiver working really we didnt even set registerReceiver()

  • @darklaker
    @darklaker Před rokem +2

    The best explanation for services I've found and it's just what I needed. I would add a handler to show a toast message from the service.
    Handler mainHandler = new Handler(getMainLooper());
    mainHandler.post(new Runnable() {
    @Override
    public void run() {
    // Do your stuff here related to UI, e.g. show toast
    Toast.makeText(getApplicationContext(), "Message from Service", Toast.LENGTH_SHORT).show();
    }
    });

  • @yeongshyhhaw9374
    @yeongshyhhaw9374 Před rokem

    how to stop..

  • @dfged9654
    @dfged9654 Před rokem +1

    czcams.com/video/bA7v1Ubjlzw/video.html Here, the service is stopped. But the service is not explicitly stopped by calling stopService() or stopSelf() from within the service itself or by another component such as an activity or a broadcast receiver. I wonder why? Thx :)

  • @shafiqsadat37
    @shafiqsadat37 Před 2 lety

  • @VoxLott
    @VoxLott Před 11 měsíci

    Why so many ads dude? I watched sum of 1.5 mins ads whole through this video

  • @mdottech6502
    @mdottech6502 Před rokem +1

    Awesome

  • @imadsaddik
    @imadsaddik Před rokem

    Thanks

  • @samiulalim1230
    @samiulalim1230 Před rokem

    Normally the codes working fine but when i add a webview in main activity then foground service stopped when apk closed. Is there any solution for it. I want to keep webview in my project and also want the foreground service

    • @Codeible
      @Codeible  Před rokem

      Hi, should still work.

    • @samiulalim1230
      @samiulalim1230 Před rokem

      @@Codeible not working after adding webview 😭😭