Bound Services on Android

  Переглядів 60,830

CodingWithMitch

CodingWithMitch

День тому

With respect to Services on Android, you have two categories:
1) Started Services
2) and Bound Services
This video is focused on bound services.
I think the best way to think of a bound service is to compare it with a server. When you start a service with the intention to bind something to it, you essentially have the intention to start a server, and bind to it with some kind of client. At it's core, you're defining a client-server interaction. The service acts as the server, and an activity acts as the client.
Note that this demo is done using the architectural design pattern known as "Model View View-Model" (MVVM).
➤Blog post on Bound Services:
↻ codingwithmitch.com/blog/boun...
➤Learn to Code Like me:
↻ codingwithmitch.com/register/
➤My Instagram:
↻ / mitch.tabian
➤Source Code:
↻ github.com/mitchtabian/Bound-...

КОМЕНТАРІ: 118
@cnsnmms3708
@cnsnmms3708 3 роки тому
I was working on a ble sensor client service which must communicate consistently with an activity and I was almost hopeless because of the android os restrictions and started service logic. You are solving a very complex issue about android services with a such simple example :) thanks
@TechMalaya
@TechMalaya 4 роки тому
I love ur tutorial when u explain it very simple to understand for me. Keep it going mitch. I just stuck using bound service in mvvm and u the only reference that I had.
@atifmbhutta
@atifmbhutta 4 роки тому
Damnn Mitch you are the best. Precise and to the point. Very well explained videos always.
@satyamsharma8240
@satyamsharma8240 2 роки тому
Love the way you made everything so simple
@Priyanshu-nq8rm
@Priyanshu-nq8rm 7 місяців тому
Thanks Mitch for an awesome tutorial ☺️
@bjugdbjk
@bjugdbjk 5 років тому
Fabulous explanation, Thank you so much, Appreciate your efforts.
@muneerhasan
@muneerhasan 3 роки тому
This video about service is best video on services. This video cleared my all doubt 💯💯. Thanks a lot Mitch
@user-hp2yi9yy3x
@user-hp2yi9yy3x Рік тому
The best tutorial on services I ever seen
@virus2028
@virus2028 2 роки тому
Good work, thanx for the lecture man. Helped a lot
@trollrar
@trollrar 5 років тому
Background thread service tutorial please :)
@hassanruben1019
@hassanruben1019 2 роки тому
Instablaster...
@developerkang3717
@developerkang3717 3 роки тому
this is exactly What i neeed!! Thank you from South korea
@kunalsahu4
@kunalsahu4 2 роки тому
Thank you so much Mitch, that very cool explanation. I request you to please create more content on Services and running task in BG
@alirzayucefaydal591
@alirzayucefaydal591 5 років тому
my mind blown :D i believe we need to go deeper and do more exercises with ur help :D
@davidfa7430
@davidfa7430 4 роки тому
Thank you for your great video. I have a question that is it possible to given a service bound by multi-activities? so that no matter which activity is running, the service will keep running without re-create?
@Since1059
@Since1059 4 роки тому
Really Really Really so nice and clean directions about bind services 😘😘😘😘
@abdulwahidmohammed6780
@abdulwahidmohammed6780 5 років тому
That's so fantastic and useful.
@codingwithmitch
@codingwithmitch 5 років тому
I appreciate the love brother :D
@carlosalvarez2506
@carlosalvarez2506 3 роки тому
Thanks for the video, it was really helpfull
@shawngardner261
@shawngardner261 3 роки тому
I enjoy your tutorials and classes. Wondering if you could update with foreground services and bringing in more clean architecture like less knowledge in the activity about the service, etc. Thanks!
@021ritvik
@021ritvik 4 роки тому
Best tutorial ever!
@mohamadnabikazemi6737
@mohamadnabikazemi6737 3 роки тому
perfect , thank you
@mirkaram548
@mirkaram548 5 років тому
Make video on Starting service in ANDROID.O and changes in O. Your explanation is amazing with architectural design.
@luvsinha1223
@luvsinha1223 3 роки тому
awesome video. learned a lot .
@ChrisAthanas
@ChrisAthanas 3 роки тому
Some engagement getting you to 100k
@aminamer5283
@aminamer5283 4 роки тому
thank you Mitch... another great video! made ma day ?
@devanshu_on_youtube
@devanshu_on_youtube Рік тому
Hey Mitch, love your tutorials! But if I may, isn't viewing a bound/started as service types (though not mutually exclusive) a bit inaccurate as a mental model? What exists is the Service class by itself - it can either be started, be started and bound to or be started as a consequence of being bound to. I think the folks have revised the documentation to reflect this representation as well.
@medicalpump9275
@medicalpump9275 4 роки тому
Dude you are awesome...
@AnkurJagani1000
@AnkurJagani1000 5 років тому
That will be great if you can make video on startForegroundServices which is must for Android O devices.
@nandandesai6524
@nandandesai6524 5 років тому
How can you run a service in a background thread? Please please make a video on it. Thanks!
@Androidmeda
@Androidmeda 4 роки тому
Really thanks for this lecture, sir. could I ask you, when to use Service, when to use IntentService, when to use JobIntentService?
@maheswaranmohan542
@maheswaranmohan542 4 роки тому
Please make a video to cover all points including started service, foreground service, Run service in background thread.....and android O..... And how to choose the correct one....
@capitalcouch5095
@capitalcouch5095 5 років тому
Hey Mitch, Great Stuff. Your content has helped me a lot in learning Android. I am starting to do videos on Android as well. I was checking your older videos, a huge improvement in video quality over time. What kind of screen recording software are you using? Thanks :)
@codingwithmitch
@codingwithmitch 5 років тому
Hey, it's camtasia.
@codingwithmitch
@codingwithmitch 5 років тому
What kind of videos will you be making? Like what topics?
@capitalcouch5095
@capitalcouch5095 5 років тому
I will start with general beginners Android topics. It seems these get the most views, to get the attraction. Eventually, stuff like building clone apps, or in-depth on a certain topic. You have some recommendation?
@codingwithmitch
@codingwithmitch 5 років тому
@@capitalcouch5095 if you are looking to maximize attention then definitely beginner stuff. The unfortunate thing about that is it gets very boring lol
@capitalcouch5095
@capitalcouch5095 5 років тому
Haha, well no pain no subs gain. Actually, I haven't seen many videos on beginners stuff but digging down in the implementation details inside android framework. What can we expect on this channel for the next year?
@dineshchoudhary8221
@dineshchoudhary8221 5 років тому
Yes please do a video on running services in a background thread
@codingwithmitch
@codingwithmitch 5 років тому
noted
@maysal-reem6457
@maysal-reem6457 3 роки тому
Great video! And could you please make some videos about the whole services thing because it's confusing
@eureka4498
@eureka4498 3 роки тому
Great leacture about service. In a bound service, I want to implement the never-ending sevice after booting the smartphone. I know that case in a started service but I don't have any clues in the bound one. Could you give me some comments?
@amitabhsarkar9571
@amitabhsarkar9571 4 роки тому
Please make a tutorial of forgroundService for android O.
@figoaranta3505
@figoaranta3505 2 роки тому
Hi Mitch, I am new in android development, where do you think is better to access a service, from view or ViewModel? Thanks
@realtimeexperience4470
@realtimeexperience4470 4 роки тому
your all videos are awesome. Can you make a complete video/split in series which has all the folder structure.. where to keep UI/ViewModel/Data/DB/Network with MVVM best practice.
@ant4197
@ant4197 5 років тому
what was the real use case of services like that? in your experience. looks quite difficult to understand if learning it on your own...
@shyam8999
@shyam8999 5 років тому
Nice work and please create a video for android oreo background services and background location
@ankitrajdwivedi996
@ankitrajdwivedi996 5 років тому
Yes we need video on background thread
@codingwithmitch
@codingwithmitch 5 років тому
noted
@jinothomas8365
@jinothomas8365 3 роки тому
@@codingwithmitch link?
@TheMikkelet
@TheMikkelet 3 роки тому
I do not understand this example, Mitch. I tried downloading the repo and run it. The app doesnt maintain state. There's no notification, like you'd see in a media app, and the app resets the timer if you swipe to close. What is the point of a service exactly? Because it doesn't seem to hold any persistent state
@szymek16s
@szymek16s 8 місяців тому
Great tutorial, I looking for solution of my problem and it's hard. I have an app which counting work time but when user closed app by accident I want that my time will be still counting cous of the service and whats happend that viewmodel updating view all the time in the same time ForegroundService do the same and my timer shows wrong value.
@HydroHeiperGen
@HydroHeiperGen 4 роки тому
@CodingWithMitch Atfrist, thank you for offering so much of your time to help noobs like me. Your Video is pretty good, but as a experienced Beginner, there are some questions left open. @25.40 As I understood the point of MVVM is to encapsulate the way of getting Data and the displaying of Data, to create a better structured code. So why do we bind the Service to the Activity and pass the ViewModel class instead of starting the Service in the Activity, but bind the service directly to the VM.class? As I understand, in your video, we create a bridge from Service to Activity, which continuous to the VM and the data later on will be send from the VM to the Activity. Can you tell me where I went wrong?
@codingwithmitch
@codingwithmitch 4 роки тому
This is actually an anti-pattern btw. You shouldn't bind services like this. Check out this article medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
@chiragprajapati3989
@chiragprajapati3989 5 років тому
nice please make more video on service
@jacekgawel
@jacekgawel 3 роки тому
Awesome tutorials.. but It is only for simple Services. What when I have a location service which returns ? I can do all that stuff like "calculate distance" "find speed" "change to LatLng" in service and do some getters methods but I thing it isn't right according to mvvm pattern. So I need to observe data in Repository somehow? There is a simple solution for this? Obviously I can get location from bounded service to activity, like you did, then viewmodel.setvalue(location). From viewmodel send to Repository, do the staff, and observe it, but it isn't right, isn't it?
@guillermoperez8250
@guillermoperez8250 3 роки тому
Cool stuff, thank you. You can get a reference to the service as soon as you bind to it, no need for the observer. Just do: MyService.LocalBinder binder = (MyService.LocalBinder) service; mService = binder.getService();
@moammarqaddafi4858
@moammarqaddafi4858 11 місяців тому
did you make a video to run service on bg thread?
@wawa3
@wawa3 3 роки тому
Wouldn't new handler and runnable be created every time the progressUpdating get onChanged this way?
@nktkumar82
@nktkumar82 2 роки тому
This is awesome.Just had one doubt. Is it not wrong to implement the "Service Connection" inside ViewModel as it is against the concept of MVVM as ViewModel should not contain anything related to Android Framework or android framework apis.
@devanshu_on_youtube
@devanshu_on_youtube Рік тому
That is the ideal case, but I think it's more of a KMM perspective, i.e. if you're targeting multiple platforms like iOS and Android using KMM, it makes sense to encapsulate all business logic in the view-model minus the Android Framework-specific stuff.
@sukhwantsingh501
@sukhwantsingh501 3 роки тому
Great sir.. please share on intent service
@shubamvirdi2907
@shubamvirdi2907 4 роки тому
Sir please make a video of integrating exoplayer using services and getting realtime feed from the service of running music.
@amirhusseinpoudineh770
@amirhusseinpoudineh770 26 днів тому
Hi Mitch Please answer this ,i want to make a call waiting app , and i want to post a notification when some one call the phone , I did it using Broad Cast Receiver but i want to do it when app is completely close . is this video what i need ?
@amremam8937
@amremam8937 3 роки тому
I would be great if you have added an example of aidl in this video
@raveen1742
@raveen1742 3 роки тому
I would love to see a service running in the background .... please make a video on it
@ravikumarmohan1415
@ravikumarmohan1415 4 роки тому
Please make a tutorial of forgroundService for android O and also on WorkManager and AlaramManager with broadcast
@ishankhandelwal741
@ishankhandelwal741 2 роки тому
Awesome
@smit9779
@smit9779 5 років тому
Great tutor
@notsure1516
@notsure1516 5 років тому
big Mitch
@olegalekos2181
@olegalekos2181 4 роки тому
I have a guestion.. Let say i want my app to run a service that uses Sensor and calculates Steps and than return each step to Activity. A. Should it be On another Thread so it wont freeze main thread? B. Should it be Bound ?
@nishantkumar-fl3cy
@nishantkumar-fl3cy 3 роки тому
Please make a video on what can we do on background service thread.
@timbraasch1514
@timbraasch1514 3 роки тому
the MVVM pattern sure is a good pattern, but i wouldnt recommend using it in a guide video since it can confuse newer programmers.
@prafulnayak1
@prafulnayak1 4 роки тому
It is a great tutorial. Can you please make a tutorial for Services for Android O or higher
@aliakkawi4759
@aliakkawi4759 5 років тому
Thanks a lot. What is the difference between Services and Android Job Scheduler? and when to use each? In one of my projects I used Android Job Scheduler to Schedule Push notifications twice a day. Thanks
@codingwithmitch
@codingwithmitch 5 років тому
Too much to say about that. You should just read internet articles. I don't have a post on the differences.
@grape99902
@grape99902 4 роки тому
Please make a background thread video :)
@pawankhandalnasirda
@pawankhandalnasirda 10 місяців тому
hey i am intested in the to run the service on background thread.
@mimrankhan9974
@mimrankhan9974 5 років тому
please make a video on threads with practical example not just print log messages
@deeprajpandey7612
@deeprajpandey7612 4 роки тому
please make a video on background thread
@thesalehmahmood
@thesalehmahmood 5 років тому
Haven't watched it yet but Is this video part of that Spotify alike app?
@codingwithmitch
@codingwithmitch 5 років тому
Nope just bound services
@sourovkarmakar3237
@sourovkarmakar3237 3 роки тому
can you please make a video on media code API of android
@gofudgeyourselves9024
@gofudgeyourselves9024 5 років тому
What are your thoughts on flutter? Will it replace native Android Development
@codingwithmitch
@codingwithmitch 5 років тому
I'm honestly not sure. I don't know enough about Flutter.
@ryszardmachmudow9175
@ryszardmachmudow9175 2 роки тому
I read ViewModel should not contain services, because Service has it's own lifecycle. Service field will lead to context leak.
@IxIAlanIxI
@IxIAlanIxI 5 років тому
This was the best tutorial I found on the internet about Bound Services, but the fact that it uses MVVM is driving me crazy, because I can not rewrite my whole project to implement MVVM. Does anyone have a version of this code that does not use MVVM? I watched the video about MVVM to try to "remove" it from the code, but unfortunately I could only get my code to stop working.
@rizwansayyed7893
@rizwansayyed7893 4 роки тому
No problem just write service binder instance or create service connection in directly activity meaning remove viewmodel and write service code in mai activity which is in viewmodel
@TropicalCoder
@TropicalCoder 4 роки тому
I would like to have seen the service callback with progress reports rather than MainActivity polling for progress.
@ramakrishnap5732
@ramakrishnap5732 4 роки тому
how to run activity in the background in the android
@ayeshazakria2215
@ayeshazakria2215 3 роки тому
pls use microphones for clear voice. thanks
@shoaibmalik9934
@shoaibmalik9934 11 місяців тому
How does the whatsapp show there call ui when someone calls
@juanpusongjr.3102
@juanpusongjr.3102 4 роки тому
awesome tutorial.. can you please give a clue how to integrate this on playing an audio.. thanks
@codingwithmitch
@codingwithmitch 4 роки тому
Boom codingwithmitch.com/courses/android-audio-streaming/
@aleksamatic9558
@aleksamatic9558 4 роки тому
what is your favorite architectural design pattern xD?
@low_samba
@low_samba 3 роки тому
sending message to a Handler on a dead thread java.lang.IllegalStateException: Handler (android.os.Handler) {2f8aa543} sending message to a Handler on a dead thread not work
@giannisfragoulis3094
@giannisfragoulis3094 3 роки тому
Make a video for infinite running servicee PLEASE
@tehleelmir4407
@tehleelmir4407 Рік тому
If you could have just explained in a simple video, what bound service is and how to implemented it that would have been much nice, using a simple example this video could have been much better and also lesser in time and code. Just like coding in flow. But still thanks :)
@pembatamang8233
@pembatamang8233 5 років тому
I put a log under aBoolean in the MainActivity and after the work is over, the aBoolean keeps changing. Why is it happening??
@codingwithmitch
@codingwithmitch 5 років тому
It's supposed to. That means the work is done
@pembatamang8233
@pembatamang8233 5 років тому
@@codingwithmitch I don't understand.... the river of true false true false kept going on in my log...is that how it works?? isn't it bad for performance.?? please explain a bit more
@codingwithmitch
@codingwithmitch 5 років тому
@@pembatamang8233 It's the runnable. It's constantly asking the service for updates. Every 100 ms. So if you put a log there, you'll see it run constantly.
@pembatamang8233
@pembatamang8233 5 років тому
@@codingwithmitchok... how do i stop it when the work is done??
@codingwithmitch
@codingwithmitch 5 років тому
@@pembatamang8233 Whoops. There was a bug. Good find. Inside the "run" method you can't use "aBoolean" because then it's final. Which defeats the purpose because it will never change. Use "mViewModel.getIsProgressUpdating().getValue()" instead of "aBoolean" inside the run method. gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
@arhamcodes
@arhamcodes 5 років тому
Please do a video on background thread.
@samuelorji8396
@samuelorji8396 5 років тому
checkout his course on Threads, It's on his website and on pluralsight also... It's pretty cool
@codingwithmitch
@codingwithmitch 5 років тому
thanks I noted your response
@VonUndZuCaesar
@VonUndZuCaesar 4 роки тому
Better: Use AndroidX WorkManager from the new Android Jetpack Library
@vennamprasad
@vennamprasad 9 місяців тому
please do video
@talatkuyuk6556
@talatkuyuk6556 2 роки тому
Very nice tutorial. But as far as I understand, two separate runnable run at the same time in the activity and in the service to show the progress. It could be better, one runnable runs at the service and inform the progress to the activity via broadcast-broastreceiver or LiveData implementation in connection with viewmodel.
@codingwithmitch
@codingwithmitch 5 років тому
******************** WARNING: BUG ************************ Thanks to @Pemba Tamang for finding this bug. Inside the "run" method of the runnable inside MainActivity, change the "aBoolean" variable to "mViewModel.getIsProgressUpdating().getValue()". If you don't do this then the runnable will continue running forever and spawning new instances. It's due to the "aBoolean" variable being final and then being used to control whether the runnable runs or not. If it's final, it will never change. So obviously the runnable will continue running forever. Here is a gist with the updated run method: gist.github.com/mitchtabian/0621da267a677cabcd1f270e3c6513fa
@pembatamang8233
@pembatamang8233 5 років тому
its Tamang
@amirs6472
@amirs6472 3 роки тому
Please make a video on what can we do on background service thread.
Model View View-Model (MVVM): Getting Started
29:12
CodingWithMitch
Переглядів 396 тис.
Digging Into Android System Services
31:58
Dave Smith
Переглядів 30 тис.
Эксперимент во Вьетнаме. Истоки зависимости
00:47
Секретная разработка КГБ! Волга «Догонялка» на V8…
1:07:10
Мастерская Синдиката
Переглядів 2,1 млн
🤡 Путін принижується перед Токаєвим
00:56
Факти тижня
Переглядів 928 тис.
Foreground Services - Android Basics 2023
22:22
Philipp Lackner
Переглядів 57 тис.
MVVM vs. MVI - Understand the Difference Once and for All
18:40
Philipp Lackner
Переглядів 18 тис.
Memory Leaks on Android
18:36
CodingWithMitch
Переглядів 56 тис.
Multiple Backstacks on Android (My Experience)
6:53
CodingWithMitch
Переглядів 12 тис.
Let, Also, Apply, Run, With - Kotlin Scope Functions
11:44
Philipp Lackner
Переглядів 91 тис.
How AI 'Understands' Images (CLIP) - Computerphile
18:05
Computerphile
Переглядів 37 тис.